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 SblandUK · Aug 02, 2014 at 08:20 PM · objectclass

NullReferenceException and script load order

My problem is that I need to have a class with a variable that holds the count of a number of entities with another class.

To try and solve the problem I have put together some code that simplifies the process:

ChildClassA.cs

 using UnityEngine;
 using System.Collections;
 
 public class ChildClassA : MonoBehaviour
 {
 
         void OnEnable()
         {
         ControllerClass.childA.entityCount++;
         }
 
         void OnDisable()
         {
         ControllerClass.childA.entityCount--;
             }
 }


 

ChildClassAData.cs

 using UnityEngine;
 using System.Collections;
 
 public class ChildClassAData : MonoBehaviour
 {
     public int entityCount = 0;

 }

ControllerClass.cs

 using UnityEngine;
 using System.Collections;
 
 public class ControllerClass : MonoBehaviour
 {
 
     public static ChildClassAData childA;
         
         // Use this for initialization
         void Awake ()
         {
             childA = GetComponent<ChildClassAData> ();                 
         }
     
         // Update is called once per frame
         void Update ()
         {
             Debug.Log ("A: " + childA.entityCount);            
         }
 
 
 }

I receive no Compiler Errors.

When running the game I get a NullReferenceException which I assume is because the child class has tried to access the childA object before it is initialized. If any more of the object are created after this error the code runs smoothly.

Therfore I would like to know how to have the instances of the child class wait for the controller class to be fully initialized before being enabled.

Comment
Add comment · Show 3
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 AlucardJay · Aug 02, 2014 at 08:59 PM 0
Share

from my understanding, Awake and OnEnable get executed before Start, so this is a bit strange. Perhaps OnEnable is not running before Start? For a test, try using an Awake function in the ChildClassA class, to see if it is that the first enable is running after Start

 void OnEnable()
 {
     ControllerClass.childA.entityCount++;
 }

$$anonymous$$ore info :

  • http://forum.unity3d.com/threads/docs-on-calling-order-in-monobehaviour.2606/

  • http://docs.unity3d.com/$$anonymous$$anual/ExecutionOrder.html

  • http://wiki.unity3d.com/index.php/Event_Execution_Order

avatar image AlucardJay · Aug 02, 2014 at 09:25 PM 0
Share

Scratch that, I derped.

So ChildClassA is running before ControllerClass, that's why the null. ControllerClass has not stored a reference to the ChildClassAData yet. From my last post, Awake and OnEnable get executed before Start.

You could try using Awake in ControllerClass ins$$anonymous$$d of Start.

avatar image SblandUK · Aug 03, 2014 at 08:58 AM 0
Share

I spotted this too. Unfortunately it has the same effect. I have edited the script above to show awake ins$$anonymous$$d.

The order of loading was as follows: Child OnEnable() Controller Awake() Child Start()

This seems to contradict the page on Execution times: [http://docs.unity3d.com/$$anonymous$$anual/ExecutionOrder.html][1]

I have found a work around which I have put in an answer below.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SblandUK · Aug 03, 2014 at 09:03 AM

As this is only a problem for the child's that are there at the start I found a work around as below:

ChildClassA.cs

 using UnityEngine;
 using System.Collections;
 
 public class ChildClassA : MonoBehaviour
 {
     public static bool initialized = false;
     
         // Use this for initialization
         void Start ()
     {
 
          if (!initialized) {  
         ControllerClass.childA.entityCount++;
         initialized = true;
             }
     }
     
         
     void OnEnable()
     {
 
         if (ControllerClass.childA != null && initialized) {
             ControllerClass.childA.entityCount++;
         }
     }
 
 
     void OnDisable()
     {
         ControllerClass.childA.entityCount--;
     }
 
 }
     

Comment
Add comment · 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

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

Extending a class 1 Answer

array of objects 1 Answer

Trying to change a subclasses' variable 1 Answer

new Monobehaviour as variable returns warning... 1 Answer

Class Casting US/JS - 'foo' is not a member of 'Object' 1 Answer


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