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);
}

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!

had this problem once try this
var guiTime = Time.timeSinceLevelLoad - startTime;