[SOLVED] How to display number of lives remaining in game?

I am creating a lives system and so far have managed to get it working. I put it in the lose collider script as that seemed to make the most sense. I have also managed to reset the ball and paddle when a life is lost. But I have managed to confuse myself terribly trying to display the number of lives left on the game screen.

I want the current number of lives remaining to display in the lives text object. I know that I will have to do this in a script, but I am just learning C# and am not sure how or where to add the code.
Any help would be appreciated.

Here is what I have so far:

Also here is the link to my lose collider script, in case it is needed.

Hi Mary,

As usual there are many ways you could approach that, but I’ll share with you what I think is the simplest solution.
I would add a script to this Lives game object, which would update its Text component at every frame, like so:

using UnityEngine;
using UnityEngine.UI;

public class LivesText : MonoBehaviour
{
    private void Update()
    {
        GetComponent<Text>().text = LoseCollider.lives.ToString();
    }
}

This isn’t the most efficient way to do it, but hopefully this can help you figure out the basic idea :wink:

For instance, the counter doesn’t need to be updated every frame, so you could replace this Update function with your own function, and call it from the LoseCollider script only when a life is lost.

Privacy & Terms