I’ve been trying to find a way to make the score stop running once my player’s health runs to zero. One comment that I read from the tutorial says to “grab a reference to the player (public Player player) in the ScoreManager script and before increasing score of 1 make an if statement checking whether the player’s health is greater than 0”. I’m still a beginner and I’m not to sure how exactly I should execute this. The code below shows my attempt:
public int score;
public Text scoreDisplay;
public Player player;
private void Update()
{
scoreDisplay.text = "SCORE : " + score;
}
void OnTriggerEnter2D(Collider2D other)
{
if (player health <= 0)
{
if (other.CompareTag("Obstacle"))
{
score ++;
Debug.Log(score);
}
}
}
Hi, you did the wrong sign in your code
When your code is at if (player health <= 0)
you are saying that if the player health is less than or equal to 0
so instead it should be if(player health >= 0)
Like @xephosmmb122 said if(player health >= 0) should have the more than or equal to operator. But I think you mean to use player.health with no space in between. In your code above you have a space between the 2. This will create an error. The component is player and it looks like you are attaching it in the editor, and it seems as though you have a variable named “health” inside the player script/component, so to access that variable, you need to use ”player.health”: