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

时光总嘲笑我的痴心妄想 提交于 2020-01-05 07:24:21

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/26934920/how-do-i-restart-my-score-reset-static-score-in-reloading-scene-in-unity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!