Here’s the code from the three relevant class files:
using TMPro;
using UnityEngine;
public class GameSession : MonoBehaviour
{
// Configuration parameters
[Range(0.1f, 10f)] [SerializeField] float gameSpeed = 1f;
[SerializeField] int pointsPerBlockDestroyed = 10;
[SerializeField] TextMeshProUGUI scoreText;
[SerializeField] TextMeshProUGUI livesText;
//State parameters
[SerializeField] int currentScore = 0;
[SerializeField] int numberOfLives = 2;
// Singleton implementation to maintain game data on level progression.
private void Awake()
{
int gameStatusCount = FindObjectsOfType<GameSession>().Length;
if (gameStatusCount > 1)
{
gameObject.SetActive(false);
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}
}
public void Start()
{
ScoreToString();
LivesToString();
}
// Update is called once per frame
void Update()
{
Time.timeScale = gameSpeed;
}
// Updates number of lives and total current score in the display area
public void UpdateDisplay()
{
LivesToString();
ScoreToString();
}
// Resets the game
public void gameReset()
{
Destroy(gameObject);
}
// Converts the score integer to a string to be displayed in the display area
public void ScoreToString()
{
scoreText.text = currentScore.ToString();
}
// Converts the lives integer to a string to be displayed in the display area
public void LivesToString()
{
livesText.text = numberOfLives.ToString();
}
// Updates current score; called in the Block.cs file when a block is destroyed
public void scoreCounter()
{
currentScore += pointsPerBlockDestroyed;
}
// Adds a life ever 200 points
public void addALife()
{
if (currentScore != 0 && currentScore % 200 == 0)
{
numberOfLives++;
}
}
// Called in LoseCollider.cs on bottom boundary collision
public void loseALife()
{
numberOfLives--;
}
// Called in LoseCollider.cs; if number of lives == 0 game ends
public int livesCheck()
{
return numberOfLives;
}
}
the LoseCollider.cs:
using UnityEngine;
using UnityEngine.SceneManagement;
public class LoseCollider : MonoBehaviour
{
string gameOver = "GameOver";
int noMoreLives = 0;
GameSession gameStatus;
Ball ballReset;
private void OnTriggerEnter2D(Collider2D collision)
{
SceneManager.LoadScene("GameOver");
// gameStatus.loseALife();
/*
if (gameStatus.livesCheck() == noMoreLives)
{
SceneManager.LoadScene(gameOver);
}
else
{
ballReset.ballReset();
}
*/
}
}
and the Ball.cs class
using UnityEngine;
public class Ball : MonoBehaviour
{
// Configuration parameters
[SerializeField] Paddle paddle1;
[SerializeField] float launchX = 2f;
[SerializeField] float launchY = 15f;
[SerializeField] AudioClip[] ballSounds;
// State parameters
Vector2 paddleToBallVector;
bool hasStarted = false;
// cached component references
AudioSource myAudioSource;
// Start is called before the first frame update
void Start()
{
paddleToBallVector = transform.position - paddle1.transform.position;
myAudioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (!hasStarted)
{
LockBallToPaddle();
LaunchOnMouseClick();
}
}
private void LaunchOnMouseClick()
{
if (Input.GetMouseButtonDown(0))
{
hasStarted = true;
GetComponent<Rigidbody2D>().velocity = new Vector2(launchX, launchY);
}
}
private void LockBallToPaddle()
{
Vector2 paddlePosition = new Vector2(paddle1.transform.position.x,
paddle1.transform.position.y);
transform.position = paddleToBallVector + paddlePosition;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (hasStarted)
{
AudioClip clip = ballSounds[Random.Range(0, ballSounds.Length)];
myAudioSource.PlayOneShot(clip);
}
}
public void ballReset()
{
hasStarted = false;
}
}
The issue I’m having is getting the LoseALife()
function to initialize when the player misses the ball with the paddle. I can’t get the script to update to minus one from the numberOfLives
variable and then reflect the new value on the display screen in-game. I also am having trouble getting the ball and paddle to reset to their starting position on a lost life.
Any suggestions would be greatly appreciated. I’m not looking for the answer just some helpful hints.