- Home /
Game manager Object reference not set to an instance of an object
I was following along with a video about a scene manager 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[i] = null;
}
updateDelegates = null;
}
if(manager != null)
{
manager = null;
}
}
}
Answer by valyard · Jun 02, 2015 at 11:41 AM
You never instantiate updateDelegates
so it's null by default and all your updateDelegates[(int)State.Reset]
calls fail.
You need to add this code somewhere:
updateDelegates = new UpdateDelegate[State.Count];
But not knowing what State
actually is it's hard to say that this code will work.
State is an enum:
updateDelegates = new UpdateDelegate[Enum.GetNames(typeof(State)).Length];
Your answer
Follow this Question
Related Questions
Toggle ON/OFF MaterialChange 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Are multidimensional arrays supported in UNET? 1 Answer
Change color of the object based on previous player's choice 0 Answers