In my game I have a timer that counts to zero. When the player reaches a goal object, (or the timer runs out), a new scene will load (either the same one or a different one.) Here’s the script:
var startTime = 30.0;
function Update () {
//The time left for player to complete level
timeLeft = startTime - Time.time;
//Don't let the time left go below zero
timeLeft = Mathf.Max (0, timeLeft);
//Format the time nicely
guiText.text = FormatTime (timeLeft);
if (timeLeft == 0.0)
{
Application.LoadLevel (1);
}
}
function OnLevelWasLoaded (level : int) {
if (level == 2) {
StartTime = 30.0;
}
}
function FormatTime (time) {
var intTime : int = time;
var minutes : int = intTime / 60;
var seconds : int = intTime % 60;
timeText = minutes.ToString () + ":";
timeText = timeText + seconds.ToString ();
return timeText;
}
With this script, the timer is running as soon as the game is loaded, even though the script is only being used in one scene (attached to a single GUI Text object.) I’m stuck…so any help would be awesome!
Well, it’s running the instant the object is in the game world due to being attached to the Update() function. As such, if you want to wait you’re going to have to either switch its location to a separate function and have the trigger set off the function, or create the text object when the trigger is set off.
Is there a way to simply force the script to restart everytime a different scene loads?
Yup. Destroy it on load and have a new one in the new scene.
Technically doesn’t every object in a scene get destroyed when a new scene loads? Since the timer script is only being used in one scene I don’t see why its effecting the other scenes in my game.
What would be the best function to use besides Update()?
It’s persisting through different levels? It could just be loading levels as the new level is destroying the old stuff. After all, it runs the result every frame.
If it’s being destroyed on load, then it technically shouldn’t be there to run the script. As such, it’s not being destroyed for whatever reason.
Run it in a while loop in a separate function. (Don’t forget to yield at the end of the loop) Have that function be called when the time == 0.0 and set up a boolean so it will not be called again. That should load the level and whack the object.
Its starting to get alittle a clearer now. Sorry for all the questions…if you haven’t guessed by now I’m not a programmer lol…I’m just starting to get my feet wet alil.
After trying your suggestions I’ve discovered I just need a way to get the timer script to restart everytime its loaded (or when another scene loads).
var startTime = 30.0;
function Update () {
//The time left for player to complete level
timeLeft = startTime - Time.time;
//Don't let the time left go below zero
timeLeft = Mathf.Max (0, timeLeft);
//Format the time nicely
guiText.text = FormatTime (timeLeft);
if (timeLeft == 0.0) {
Destroy(this);
Application.LoadLevel (1);}
}
With this, if I trigger the end of the scene manually the scene will restart correctly, but the timer picks up where it left off (instead of starting over).
When the timer runs out, whenever I choose to load the scene again while the game is running, the timer is always at 0.
So basically my script just needs to restart from the beginning every time a scene loads right? Thanks again for all the help, your saving my @ss here 8)