- Home /
Resetting a Scene
I have a MainMenu, a game screen, and a Win Screen. The game Screen consists of the player, falling enemies, and a countdown timer/score counter.
The game ends when the timer reaches 0:00 and it moves over to the WinScreen. The winScreen has a button to the main menu, but when I click to Play again, it takes me to the game screen which has not been reset. The Score does not start at 0, and the timer keeps on going down into the negatives.
How can I completely reset the scene so that everything starts the way it would when loaded for the first time?
Thanks
EDIT: Everything except for the score and timer seem to be resetting. The timer remains counting even when the scene has been switched.
var startTime : int; var restSeconds : int;
var roundedRestSeconds : int; var displaySeconds : int; var displayMinutes : int;
var countDown : int;
function Update () { //make sure that your time is based on when this script was first called //instead of when your game started startTime = 12;
var guiTime = Time.time - startTime;
restSeconds = countDown - (guiTime);
//display messages or whatever here -->do stuff based on your timer
if (restSeconds == 60) {
print ("One Minute Left");
}
if (restSeconds == 0) {
print ("Time is Over");
MouseFollow.playerScore = 0;
Application.LoadLevel(2);
}
//display the timer
roundedRestSeconds = Mathf.CeilToInt(restSeconds);
displaySeconds = roundedRestSeconds % 60;
displayMinutes = roundedRestSeconds / 60;
guiText.text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds);
//GUI.Label (Rect (350, 25, 100, 30), text);
}
Answer by · Oct 27, 2010 at 05:30 AM
You could use Application.LoadLevel to reload the level/scene. This would revert all the variables to their default, and seems to be a popular solution (based on answers to similar questions).
Your variables are mostly likely being reset, but the way you're calculating time remaining won't work with the current implementation.
startTime = 12; // the problem lies here
- Every frame, you're setting 'startTime' to 12.
- You're then calculating 'guiTime' by subtracting '12' from Time.time.
- Time.time is the time in seconds since the start of the game. When you reload the level, the time since the start of the game does not get reset.
You've actually hit the nail on the head with your comment:
//make sure that your time is based on when this script was first called
//instead of when your game started
'function Start ()' is called just before any of the Update methods is called the first time. You can store the current Time.time to startTime at this point, and then your calculations should be correct!
function Start ()
{
startTime = Time.time;
}
You'll also need to remove 'startTime = 12;' from the Update() loop.
Hope this clears things up for you!
That's how I load the levels, but when it loads, it's not reset.
That would only happen if you have objects marked as DontDestroyOnLoad, or using static variables. Either way, theres no way to reset them automagically - you'll need to destroy them or reset them yourself
The only static variable I have is the player score, I've managed to reset it within another script by changing its value manually. That's all fine now, but the timer still remains counting down into the negatives. I don't have any objects marked as DontDestroyOnLoad.
Could you please post the code for your timer in your question so we can take a look at it?
It's weird that LoadLevel isn't resetting things but how about just resetting the timer when the level loads?
Answer by el-santia93 · Aug 28, 2013 at 01:55 PM
had this problem once try this var guiTime = Time.timeSinceLevelLoad - startTime;