- Home /
My life counter is not updating
When I load a scene my life counter only works one time before it blocks. I don't know what's going on.
The script of the life counter:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LifeManager : MonoBehaviour
{
public int startingLives;
private int lifeCounter;
private Text theText;
public GameObject gameOverScreen;
public PlayerController2D Player;
public float waitAfterGameOver;
// Start is called before the first frame update
void Start()
{
theText = GetComponent<Text>();
lifeCounter = startingLives;
Player = FindObjectOfType<PlayerController2D>();
}
// Update is called once per frame
void Update()
{
if(lifeCounter == 0)
{
gameOverScreen.SetActive(true);
Destroy(GameObject.Find("Canvas"));
}
theText.text = "x " + lifeCounter;
/*
if(gameOverScreen.activeSelf)
{
waitAfterGameOver -= Time.deltaTime;
}
if(waitAfterGameOver < 0)
{
SceneManager.LoadScene("Menu");
Destroy(GameObject.Find("CanvasPermanent"));
}*/
}
public void GiveLife()
{
lifeCounter++;
}
public void TakeLife()
{
lifeCounter--;
}
}
Answer by alexianphilosophy · Feb 04 at 03:14 AM
The route you're taking to have a life counter is kind of roundabout. Honestly, I would just reference a text object from the player controller itself, and then update the health from there. To your issue, where it only looks like it updates the life once, I think that maybe your text's default text LOOKS like it was updated, but it was never updated in the first place. To reduce the amount of confusion in your code, you should probably keep the player script updating the UI as well as making the player be the one in charge of setting the "Game Over" screen, etc... Another thing you should avoid is destroying your canvas. First, the way you find the canvas is kind of unreliable, and if you really want to destroy it, you should set it in the inspector, rename it to something specific, or tag it, but honestly you shouldn't destroy it. If you want it gone, maybe try disabling it with gameObject.SetActive();
(make sure that gameObject is a reference to the canvas). You could reduce a lot of the code you have here by simply using the player's script to update the UI.
Try doing this: delete this script and add a variable to your player controller called lifeText
or something. Text is fine, but you should import TextMeshPro (it's much better than the default Unity text, it should be really accessible as well: the editor has the ability to directly import it). Regardless, you add that variable to your player controller and then update the text from the update function there.
You should also "check for death" from your player controller. Make sure to use if(health <= 0)
in case your health jumps over zero and avoids the code: always have that as a safety.
If you really want to disable your health canvas, make sure to specifically reference it. "Canvas" is too general, and it looks like you have multiple, which could lead to errors.
Note: if you want to check if something is running continuously, just add a quick Debug.Log("It works.");