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
1
Question by drorriov · Apr 03, 2015 at 02:32 PM · c#unity 5unity 4

Can't access a Singletons instant.

A question regarding Singleton, i guess it should be simple and i am doing something wrong.. the code:

GameManager.cs

 using UnityEngine;
 using System.Collections;
 
 public class GameManager : MonoBehaviour {
 
  
         public static GameManager Instance { get; private set; }
         
         private void Awake() {
             if (Instance != null) {
                 DestroyImmediate(gameObject);
                 return;
             }
             Instance = this;
             DontDestroyOnLoad(gameObject);
         }
 
         public void  DoSomething(string text)
         {
             
             print(text); 
         }      
     }
 

test.cs (test the singletone)

 using UnityEngine;
 using System.Collections;
 
 public class test : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
 
         GameManager.Instance.DoSomething("got it");
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }
 


the question is: why do i get an error: NullReferenceException: Object reference not set to an instance of an object test.Start () (at Assets/test.cs:9)

this line: GameManager.Instance.DoSomething("got it"); Why can't i access it?

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

2 Replies

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

Answer by supernat · Apr 03, 2015 at 02:34 PM

Start calls are not in a guaranteed order, so you're lucky you caught this bug. If the GameManager::Start() method was called before the test::Start() method, it would go unnoticed and possibly crash on different platforms. Move the GameManager::Start() code to the GameManager::Awake() method instead, because by the time you get to the Start() call, Unity guarantees that all Awake() methods have been executed on every object.

Secondly, just make sure you have actually attached the GameManager script to an object in the scene.

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 drorriov · Apr 03, 2015 at 02:40 PM 0
Share

"Secondly, just make sure you have actually attached the Game$$anonymous$$anager script to an object in the scene" SOLVED IT! :) Also changed Start() to Awake() (changed also in original post to Awake)

avatar image
3

Answer by _joe_ · Apr 03, 2015 at 02:37 PM

This problem is probably due to the fact that the Start of class test is being executed before your GameManager class.

You can fix it in 2 ways:

  • Either change your GameManager Start code and place it in Awake()

  • Or go to Edit -> Project Settings -> Script Execution Order, and add GameManager.cs and test.cs and make sure that GameManager is above (executes before) test.

Comment
Add comment · Show 2 · 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 supernat · Apr 03, 2015 at 02:41 PM 0
Share

+ 1 for the Project Settings reference. I didn't know about that! You can possibly also move the Game$$anonymous$$anager object to the top of the hierarchy in the Hierarchy view, not sure if that will change the call order.

avatar image drorriov · Apr 03, 2015 at 02:42 PM 0
Share

Can't i just add Game$$anonymous$$anager.cs with value 100? if i have like 20 more scripts, i need to add them all to the "Script Execution Order"?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Can anyone help with loading issues when button is clicked? 0 Answers

Kongregate API not connecting C# 2 Answers

How to get a dialog box on selecting a 3d object ? 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