- Home /
Reset a scene
I'm currently working on a card game. When all cards are cleared, or the player finishes the game, i'm enabling a button to go back to the main menu or restart the level via:
Application.LoadLevel ("MainMenu");
or
Application.LoadLevel (Application.loadedLevel);
When i reload the level again, nothing is resettet. The time / score / cards... everything is still there.
What i've read, Application.LoadLevel should reset everything, unless i declare something as static or use "DontDestroyOnLoad" in the Awake function. (Which i've done nowhere in the entire scene)
Another funny thing is, that when the scene is reloaded, i get tons of
UnassignedReferenceException: The variable movesGUI of 'cardSelection' has not been assigned.
You probably need to assign the movesGUI variable of the cardSelection script in the inspector.
errors...
I've declared "movesGUI" as public and dragged a GameObject with a GuiText on it. I'm also doing this with 3 other GUIText elements (they don't throw any errors on reload...)
Do you have any idea, why my scene isn't completley reset?
Is there any way to force reset a scene?
Answer by Xtro · Aug 15, 2013 at 08:24 PM
You do correct to reset the level.
I think the problem is about the location of the data(time, score,cards,...)
Where and How do you store those values ? In static variables ? In a ScriptableObject as a prefab??
No, i'm sure i'm not using static on any variables / classes / etc...
Time is handled like this:
public float timeLeft = 180.0f;
public float time = 180.0f;
....
void Update () {
timeLeft = time - Time.time;
timeGUI.text = "" + (int)timeLeft;
...
}
As you can see i'm initializing the time / timeLeft variable, so it should be resetted to that value?
I've rechecked my game... I've called the "GameWon()" function in the beginning for debug purposes, so i have missed that most of the reloading works, sry for that.
The Cards are correctly replaced, and shuffled again. The Score is reset, but the time is not.
And i still get that awkward error which i posted above.
The game also doesn't respond anymore, but this could be due to the unassigned reference error....
Edit:
I've done further research... I've removed the movesGUI variable from my script and tried again. The error is gone, but so is the gameObject holding the scripts to generate the Cards and to sleect them...) I wonder why the cards are generated and placed anyway....
I'm not using GameObject.Destroy anywhere... really strange...
Time.time is the start since the game initialized / simulation started, not the start from the last scene load. When I calculate time I simply do this:
timeVariable += 1*Time.deltaTime;
It's frame independent, it never fails, and is easy to read when functions gets nasty.
Another thing you can do is to set time to Time.time+180.0f when level loads, in Start or Awake preferably.
void Start () {
time = Time.time+180.0f;
}
Hope it works out!
Are you using an 3rd party addon something like "Persist Playmode" ??
That kind of addons helps you to modify the scene objects in playmode persistently. If you have that addon it could save the inspector values with the runtime values. And they can be reloaded with the saved value on restart.
Answer by stoward94 · Mar 18, 2014 at 10:27 PM
I was having a very similar problem with a scene I was working on. It was a simply race course in which allowed you to replay the track at the end. I was using Time.time in order to calculate and store the total time for the race along with other things. However when I reloaded the scene the Time never reset hence would never enter in to given condition for if statements and so on.
What fixed this for me was the following:
void Start() {
totalTime = Time.timeSinceLevelLoad;
}
Instead of
totalTime = Time.time;
This wouldn't technically reset the time, but just use the time since the level reloaded or simply started.
Hope this helps :)