- Home /
Timer not working as expected.
I have a guiText in my scene with this script attached to it:
var time = 0; var timeLastRound = 0;
function Start () { timeLastRound = time; }
function LateUpdate () { time = Time.time - timeLastRound; guiText.text = ""+time; }
The guiText displays the time just fine, but when I click my restart button (reloads the scene) or change level difficulty (reloads the scene with some variables tweaked) the timer doesn't reset properly. Am I wrong in assuming that every time you load a scene the Start () function will be run first? What am I doing wrong ;p
Please, can you highlight your code. See the editing-help for more details: http://answers.unity3d.com/editing-help
Answer by Bunny83 · Mar 05, 2011 at 05:48 PM
You are right, Start is called when a new scene is loaded and the object is created. Objects that resists LoadLevel (DontDestroyOnLoad), will not have their Start functions called.
Your little mistake is that you didn't "reset" your timeLastRound
variable with the current time. You need to set the actual time.
function Start () {
timeLastRound = Time.time;
}
h such a stupid mistake, thanks a lot! And I'll make sure to highlight my code next time. People like you make this site awesome! ^^
Your answer
Follow this Question
Related Questions
Collecting items doesn't reset when loading a new level? 2 Answers
Reset a timer? 0 Answers
need to refind script execution order from before crashing without touching to other settings 0 Answers
how to reset time 2 Answers
How do I Reset Scene 1 Answer