I’m trying to stop a timer when it is kept to another scene.
What I want to do is creating a scene that can calculate the score. And The time is part of the score so I have to stop it.
I’ve already add DontDestroyOnLoad in the timer script. What I need to do is only stop the timer from keeping going when it appear in another scene.
Is there any idea? :?
Here is the timer:
var timeText : GUIText;
var startTime = 30.0;
function Awake () {
DontDestroyOnLoad (this);
}
function Update () {
timeLeft = startTime - Time.time;
timeLeft = Mathf.Max (0, timeLeft);
TimerText = FormatTime (timeLeft);
if(timeLeft==0)
Application.LoadLevel("NoTime");
}
function FormatTime (time) {
var intTime : int = time;
var minutes : int = intTime / 60;
var seconds : int = intTime % 60;
if(seconds<10)
secondsString="0"+seconds;
else
secondsString=""+seconds;
if(minutes<10)
minutesString="0"+minutes;
else
minutesString=""+minutes;
var prefix = ":";
timeText.guiText.text = minutesString + prefix + secondsString;
return timeText;
}
You can find what scene index you’re on with Application.loadedLevel and use that so that your script behaves differently between scenes.
However, if you’re just trying to pass scores between scenes I would use playerprefs. Create two scripts, SetScore and GetScore, in your first scene use SetScore to store the score in playerprefs, in the second scene use GetScore against playerprefs for the identity you set for the score key.
I don’t understand so clearly. If I use PlayerPrefs, is there any possibility to grab a number (time) when the scene end right away and do not increase some seconds while loading another scene?
Thanks for reply, GotCakes.
Because of we have no time to change too much script, if there is still other ways to accomplish, I’ll appreciate.
If I’m understanding right, all you’d need to do is call PlayerPrefs.SetInt(“TimeLeft”, timeLeft); just prior to any scene change. When the next scene finishes loading you can grab the time that was left with PlayerPrefs.GetInt(“TimeLeft”); and use that to calculate the score. You won’t need to use DontDestroyOnLoad then. Example below will store the timeLeft (zero) into playerprefs which you can access from any scene in your application.
Sorry in advance if that’s not quite what you’re after
var timeText : GUIText;
var startTime = 30.0;
function Awake () {
DontDestroyOnLoad (this);
}
function Update () {
timeLeft = startTime - Time.time;
timeLeft = Mathf.Max (0, timeLeft);
TimerText = FormatTime (timeLeft);
if(timeLeft==0) {
PlayerPrefs.SetInt("TimeLeft", timeLeft);
Application.LoadLevel("NoTime");
}
}
function FormatTime (time) {
var intTime : int = time;
var minutes : int = intTime / 60;
var seconds : int = intTime % 60;
if(seconds<10)
secondsString="0"+seconds;
else
secondsString=""+seconds;
if(minutes<10)
minutesString="0"+minutes;
else
minutesString=""+minutes;
var prefix = ":";
timeText.guiText.text = minutesString + prefix + secondsString;
return timeText;
}
Thanks for helping me. I am trying this a little hardly just because I don’t have a good way to let the timer know when is the end of the stage.
In fact, if player arrive the goal, the stage will go to score scene even if the time is not up.
I tried to add a script to the goal:
function OnTriggerEnter () {
gameObject.BroadcastMessage ("Goal");
Application.LoadLevel ("CountScore");
}
Despite the name, gameObject.BroadcastMessage (“Goal”); will only send a message to itself (and it’s children). is the timer script on the same object?
var scoreTimeText : GUIText;
function Update () {
timeLeftInt=PlayerPrefs.GetInt("TimeLeft");
scoreTimeText.guiText.text=timeLeftInt + "sec.";
}
It seems to be wrong somewhere. Now The GUIText can work but the PlayerPrefs always shows 597 sec. (I set the timer 600 sec.) It looks like the PlayerPrefs GetInt only after played 3 seconds and then stop at that time.
How can I fix it? Help…
Sorry for doing some mistake myself…
I try again today and find out the timer I set had not be saved. So I put it to the “Goal” and it works!
Just remind that timer GUIText cannot be some DontDestroy object’s children, or it will still stay on screen if change scene. (If use PlayerPrefs, dont need to keep the timer anymore.)