1

I have a simple script that reloads my scene. The function is called by a button OnClick() event.

public void ReloadScene(){
    SceneManager.LoadScene (SceneManager.GetActiveScene ().name);
}

This function should restart the current scene. It does what I expected it to, but with one problem: most scripts don't work.

The only scripts that work are the Audio Source, and another simple script that I have written. I get no errors when I just press "play".

I am using Unity 5.6.0b9 Personal.

rene
  • 41,474
  • 78
  • 114
  • 152
yummypasta
  • 1,398
  • 2
  • 17
  • 36
  • hey Yummy, it's possible you're facing the single biggest confusion in Unity: you probably need to do this - https://stackoverflow.com/a/35891919/294884 – Fattie May 27 '17 at 15:10

1 Answers1

1

Reloading a scene does reset the variables unless you are using static variables. A new instance of an object would reset each object to their initial state. Static variables are not destroyed as they do not belong to an instance. You can reset those manually.

DontDestroyOnLoad() is a little different. It asks Unity not to destroy an object if a new scene is laoded. So these objects won't get destroyed (reset).

If you have dynamically added some scripts, then they will get destroyed as well. Unless, they are added to a DontDestroyOnLoad() parent.

Hasan Emrah Süngü
  • 3,488
  • 1
  • 15
  • 33
  • Thats unfortunate... is there an easy way to reset these without going through all the variables? – yummypasta May 26 '17 at 01:36
  • @yummypasta, I have written a few cases. Which one resembles / matches yours? I can write in detail if i know which one best fits your case. – Hasan Emrah Süngü May 26 '17 at 01:37
  • Currently, I only have scripts added in the editor (not through code). I have no `DontDestroyOnLoad()` GameObjects. It doesn't seem to be a problem with the variables, but some of my scripts don't seem to be `Update()`ing. – yummypasta May 26 '17 at 02:45
  • @yummypasta, I created scene, put a cube, added a script which rotates it. Added a button and attached a script, which onCLick() releoads the scene. And could not reproduce your problem. A little more code would help. – Hasan Emrah Süngü May 26 '17 at 03:32
  • I just found out that my problem had nothing to do with re-loading the scene. I was just setting TimeScale to 0! Oops... Sorry for the confusion. – yummypasta May 27 '17 at 14:01
  • hey yummy - as a rule, never change *timescale* in Unity, it's just not how you do it – Fattie May 27 '17 at 15:11