- Home /
Stop score from increasing over time on player death
I have a very simple script for my score: it increases by 1 every second and by a specific amount everytime an enemy is destroyed. If the player is touched by an enemy, the player's game object gets destroyed. How would I stop the score from increasing over time after the player is destroyed?
This is the script for the score:
public class ScoreProgression : MonoBehaviour
{
Text scoreText;
public int enemiesKilledScore;
int totalScore;
void Start()
{
scoreText = GetComponent<Text>();
}
void Update()
{
totalScore = (int)Time.time + enemiesKilledScore;
scoreText.text = totalScore.ToString();
}
}
Answer by JenoahKers · Nov 11, 2019 at 07:26 PM
You can say that you have a boolean which is true whenever your player is alive and false when he is not. Then you have an if-statement in your update method where the condition is your boolean and where the statement contains your score increment code:
void Update(){
{
if(playerIsAlive){
totalscore = (int)Time.time + enemiesKilledScore;
scoretext.text = totalScore.ToString();
}
}
From your player script, you access this script and change the playerIsAlive boolean to false on your die method.
With this, you should stop the score incrementation whenever the player dies