Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by yamiko · Jun 02, 2015 at 10:55 AM · c#unity 5arraydelegate

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;
         }
     }
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Jun 02, 2015 at 12:09 PM 1
Share

State is an enum:

         updateDelegates = new UpdateDelegate[Enum.GetNames(typeof(State)).Length];

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges