When I reload a scene, I get a Null Reference Exception which I am tracking down. But I thought when Application.LoadLevel executes it destroys the current scene, so can’t I then reload the scene as if it was the first time?
In my case the Menu scene is the first scene after the Splash/Loading screen that occurs right after loading the app.
The reload path is:
Menu scene -> Play button -> Level 1 scene -> Pause button -> Menu button-> Menu scene -> Play button -> Level 1 scene.
When I get to the Level 1 scene the second time, I get the Null Reference Exception. I noticed that loading Level 1 the second time occurred very fast. Is there something else I need to execute, before loading Level 1 the second time, like to flush the cache, etc?
The common way to flush old scenes is to have a loader scene into which you load which will load the next scene you want to load.
though reloading the same hsouldn’t lead to null exceptions unless you have dontdestroyonload objects that mess themself up when they are added the second time
I figured out what was causing the problem, but I don’t understand why and I hope someone knows the reason.
In the script attached to the Main Camera in Level1, I had the following code;
static var SpriteArray : Array = new Array();
var instanceGO : GameObject;
function Awake(){
SpriteArray.Add(Instantiate(prefabSprite, Vector3(0,0,0), Quaternion.identity));
instanceGO = SpriteArray[0];
}
This worked fine the first time Level1 was loaded but the second time Level1 was loaded, I got a NullReferenceException on instanceGO. When I replaced the above code with
static var SpriteArray : Array = new Array();
var instanceGO : GameObject;
function Awake(){
instanceGO = Instantiate(prefabSprite, Vector3(0,0,0), Quaternion.identity);
SpriteArray.Add( instanceGO );
}
then everything worked fine.
Does anyone know why the first version didn’t work on the second loading of Level1?
you have used static Arrays and after reloading the scene you have add a new Object to Array. The first Array 0 exist, but the object is deleted on reloaded scene.
You must call SpriteArray.Clear() before you reload the scene.
I’ve got a really simple game I made from a tutorial, and a main menu in another scene I made from another tutorial. Clicking ‘Start Game’ in the main menu goes to the game, and when you die it pulls up a menu with an option to go back to the main menu. If you go back to the main menu, and then try to start the game again it throws a MissingReferenceException. From what I’ve been able to figure out, it’s throwing the exception on the first place that a script is referencing any object in the scene.
I get that it destroys everything in the scene when loading a new scene, but why can’t it recreate the objects later? Or if it is recreating them, why can’t it find them?
Ooh, I figured out what the problem was. The game tutorial had me make a static class with events in it which are used by a bunch of objects. It’s binding to the events with references to the objects, but then when the event was getting triggered after returning to the game it was trying to call those functions bound to the events from objects which had been destroyed.
To get it to work I made a function on the static class which clears out all of the bindings on its events. I’ve got it calling it right before I leave the level.
I know this is an old post, but I just ran into this problem. On reloading a level, I was receiving null errors on an object that was destroyed but was subscribing to an event, even though the object was unsubscribing from the event in OnDisable().
To solve the issue, I am clearing the event bindings on the particular event manager.