- Home /
Null Reference Exception when reload scene
Hello. I have a really burning question. Whenever i reload a scene or reopen a scene after exiting a another scene I keep getting null reference exceptions on my variables. Basically i am making a racing game so there will be a lot of scene switching. currently I have 5 scenes: 1 splash scene, 1 garage scene and 3 race scenes. The game is programmed to go to the garage, the user picks a race scene, then when the race is is won(or lost or quit) the user presses a button to go back to the garage scene. Whenever i reload the race scene or go back to the garage scene i get the null reference exception.
I don't have any singletons, toolboxes, static classes or static variables. None of my scripts use DontDestroyOnLoad()
. Most if not all of my scripts in the scene use Awake()
and Start()
to set the variables and reference each other at the start of the scene and then they do their business. I've when through about 12 forums and Unity Answers pages and I don't understand anything even though most of them are marked as answered. Some recommend manually setting variables to null and some say to unsubscribe from event handlers, but I don't think I subscribed to any in the first place. I even tried using SceneManager.UnloadScene
before using SceneManager.LoadScene
. Can someone please help me.
It could be the loading order of your scripts. For example in a Start method of script1 (lets call it Start1) you may be trying to access variable that have its value set in the Start method of script2 (Start2). But there is no guarantee that Start2 has been already called when Start1 is running. This can be avoided by carefully selecting if something should be placed in Awake or in Start. The last Awake method in your scene is always called before the first Start method. (Or you can use Edit -> Project settings -> Script Execution Order, but I wouldn't use that, because I$$anonymous$$O that just postpones the problem to return as bigger one)
Hi Nikaas and thanks for your response. But i don't think its the loading order as i have played through the scenes and the others like it using the same scripts and different variables with no errors. Its whem the scene reloads that the error arises as Unity says I'm not referencing an object.
You are using unity 5.3? Try using Application.LoadLevel
ins$$anonymous$$d.
Why should i use Application.LoadLevel
ins$$anonymous$$d of the Scene$$anonymous$$anager.LoadScene
. What is the difference? Isn't Application.LoadLevel
obsolete?
I tried using Application.LoadLevel
the same error occurs...
Please post the script(s) in which the errors occur, and the error messages. Without relevant informations, we can only make wild guesses.
Answer by JoshBaz · Aug 02, 2016 at 08:53 PM
I fixed it. What I did was set references to the variables in the editor before play. And then I also added some code like this:
Transform example;
void Awake() {
if(example == null){
example = GameObject.Find("Example").GetComponent<Transform>();
}
}
Your answer
Follow this Question
Related Questions
Variables are resetting after loading the play scene 1 Answer
My game breaks when I load it from another scene (menu scene). 0 Answers
Can I use one script to toggle between scenes? 1 Answer
Scene takes long to load only first time 2 Answers
Can't find game object / script after scene re-enter 1 Answer