Manash Yes
Manash Yes

Reputation: 15

How do I restart my score (reset static score) in reloading scene in unity?

How do I restart my score when I reload a screen

public class KeepingScore: Monobehaviour;

public static int Score;

I also have score set as whenever I click on an object, the object is destroyed and gives me a point.

void OnMouseDown()

KeepingScore.score += 1;

Destroy();

I also have a timer where when I run out of time, scene switches to level select menu, where I click on the level again (ie level 1), but then I still see my score back how it was. I know it's static therefore It's still the same, are there any method to reset the value to zero every time I reload the level. Thank you

Upvotes: 1

Views: 3426

Answers (1)

Ricardo Reiter
Ricardo Reiter

Reputation: 581

You can implement the MonoBehaviour.OnLevelWasLoaded(int) function.
It called every time that a level is loaded.

Example

void OnLevelWasLoaded(int level) {
    KeepingScore.score = 0;
}

Check at the docs: http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnLevelWasLoaded.html

Upvotes: 6

Related Questions