Debug.Log not showing message when lives reach zero or damage is taken,Debug.Log not showing message when lives reach zero or damage taken

Two situations related to Debug.Log. I’m sure they are quite simple and I just don’t have the right knowledge.

  1. I want the debug log to tell me “Game Over” when my “lives” reach 0 or less.

When my “Lives” do reach 0, nothing shows up or happens

Here is the code:

public class GameManager : MonoBehaviour
{

//score count
private int score;
//connecting the UGUI score Canvas to the script
public TextMeshProUGUI scoreText;

//variable for lives
private int lives;
//connecting the UGUI life Canvas to the script
public TextMeshProUGUI lifeText;

// Start is called before the first frame update
void Start()
{
    //score function
    score = 0;
    lives = -5;
    UpdateScore(0);
    LifeCount(0);


}

// Update is called once per frame
void Update()
{
    //game over if lives below hit zero

    if(lifeText.text <= "0")
    {
        Debug.Log("GAME OVER");
    }
}

Situation #2

I want a message to appear saying “Damage Taken” when my player loses a “life” or his lives total is decreased. The object is being destroyed on collision as it should, though the message is not appearing. Any suggestions?

//method for destroying objects on collision
private void OnTriggerEnter(Collider other)
{
// enemies = GameObject.FindGameObjectsWithTag(“Animal”);

    if (other.CompareTag("Player"))
    {
        //Destroys object that this script is attached to
        Destroy(gameObject);

        //decrease lives
        gameManager.LifeCount(damage);

//life lost debuglog
        Debug.Log("Damage taken");

,Two situations related to Debug.Log. I’m sure they are quite simple and I just don’t have the right knowledge.

  1. I want the debug log to tell me “Game Over” when my “lives” reach 0 or less.

When my “Lives” do reach 0, nothing shows up or happens

Here is the code:

public class GameManager : MonoBehaviour
{

//score count
private int score;
//connecting the UGUI score Canvas to the script
public TextMeshProUGUI scoreText;

//variable for lives
private int lives;
//connecting the UGUI life Canvas to the script
public TextMeshProUGUI lifeText;

// Start is called before the first frame update
void Start()
{
    //score function
    score = 0;
    lives = -5;
    UpdateScore(0);
    LifeCount(0);


}

// Update is called once per frame
void Update()
{
    //game over if lives below hit zero

    if(lifeText.text <= "0")
    {
        Debug.Log("GAME OVER");
    }
}

Situation #2

I want a message to appear saying “Damage Taken” when my player loses a “life” or his lives total is decreased. The object is being destroyed on collision as it should, though the message is not appearing. Any suggestions?

//method for destroying objects on collision
private void OnTriggerEnter(Collider other)
{
// enemies = GameObject.FindGameObjectsWithTag(“Animal”);

    if (other.CompareTag("Player"))
    {
        //Destroys object that this script is attached to
        Destroy(gameObject);

        //decrease lives
        gameManager.LifeCount(damage);

//life lost debuglog
        Debug.Log("Damage taken");

In your first situation you check if your lifeText is less or equal to another string (text):

 if(lifeText.text <= "0")  

You should instead check if your lives (int) (this is an actual number) is less than 0. So like this:

If(lives <= 0)

Your second case should work normal I think, can you give a bit more context?

Hi @jd_unity711,

There is no way to do mathematical calculations with String. When the variable value is in double quotes, it means that it is a String, in Programming Language String are values of the text type, so the text property of TextMeshProUGUI is a String.

For your case you should do the calculation using int

     private int lifePoints;

     void Update()
     {
         if(lifePoints <= 0)
         {
             Debug.Log("GAME OVER");
         }
     }

To display the result on the player’s screen, do this:

using TMPro;
using UnityEngine.UI;

public TextMeshProUGUI lifeText; // To show on the screen
private int lifePoints; // Calculate

	void Start()
	{
		lifePoints = 10;
	}
    void Update()
    {
		if(lifePoints <= 0)
		{
			lifeText.GetComponent<TextMeshProUGUI>().text = "Game Over";
		}
    }

Good luck and don’t give up