I was following along with a video about a [scene manager][1] and some other tips.
I am getting an error when I run it. “Object reference not set to an instance of an object” for the line with
updateDelegates[(int)State.Reset] = UpdateSceneReset;
I am still fairly new to c# so I am having trouble finding out what reference is not set to an instance.
using UnityEngine;
using System.Collections;
public class Manager : MonoBehaviour
{
public static Manager manager;
private string currentScene;
private string nextScene;
private AsyncOperation resourceUnload;
private AsyncOperation sceneLoad;
private enum State { Reset, Preload, Load, Unload, Post, Ready, Run, Count };
private State state;
private delegate void UpdateDelegate();
private UpdateDelegate[] updateDelegates;
// Use this for initialization
public void Awake ()
{
Object.DontDestroyOnLoad(gameObject);
manager = this;
// the error is for the line below
updateDelegates[(int)State.Reset] = UpdateSceneReset;
updateDelegates[(int)State.Preload] = UpdateScenePreload;
updateDelegates[(int)State.Load] = UpdateSceneLoad;
updateDelegates[(int)State.Unload] = UpdateSceneUnload;
updateDelegates[(int)State.Post] = UpdateScenePost;
updateDelegates[(int)State.Ready] = UpdateSceneReady;
updateDelegates[(int)State.Run] = UpdateSceneRun;
nextScene = "Main Menu";
state = State.Reset;
}
public static void SwitchScene(string scene)
{
if(manager != null)
{
if(manager.currentScene != scene)
{
manager.nextScene = scene;
}
}
}
protected void Update()
{
if(updateDelegates[(int)state] != null)
{
updateDelegates[(int)state]();
}
}
// GC old scene
private void UpdateSceneReset()
{
System.GC.Collect();
state = State.Preload;
}
// Start loading the new scene
private void UpdateScenePreload()
{
sceneLoad = Application.LoadLevelAsync(nextScene);
state = State.Load;
}
// load the scene and show load progress
private void UpdateSceneLoad()
{
if(sceneLoad.isDone == true)
{
//sceneLoad = null; // ?
state = State.Unload;
}
else
{
// show progress
}
}
private void UpdateSceneUnload()
{
if(resourceUnload == null)
{
resourceUnload = Resources.UnloadUnusedAssets();
}
else
{
if(resourceUnload.isDone == true)
{
resourceUnload = null;
state = State.Post;
}
}
}
private void UpdateScenePost()
{
currentScene = nextScene;
state = State.Ready;
}
private void UpdateSceneReady()
{
System.GC.Collect(); // dont use if you need loaded assets that are initially unused
state = State.Run;
}
private void UpdateSceneRun()
{
if(currentScene != nextScene)
{
state = State.Reset;
}
}
public void OnDestroy()
{
if(updateDelegates != null)
{
for(int i = 0; i < (int)State.Count; i++)
{
updateDelegates *= null;*
-
}*
-
updateDelegates = null;*
-
}*
-
if(manager != null)*
-
{*
-
manager = null;*
-
}*
-
}*
}