Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by TheGameLearner · Jul 23, 2018 at 04:58 AM · nullreferenceexceptionaccessaccessing from any scriptnull referencenull exception

referring static variable of existing script gives null

Hello guys, I got 2 scripts lets say MenuController and BodyModController. I have got tons of other scripts that works fine with same access ways so a bit unsure.


public class MenuController : MonoBehaviour
{
    public static MenuController menuControllerRef;
    Start()
    {
        fun1();
    }
    private fun1()
    {
        BodyModController.bodyRef.menuRef = this;
        BodyModController.bodyRef.MenuEnable();
    }
}


public class BodyModController : MonoBehaviour
{
    public static MenuController menuRef;
    public static BodyModController bodyRef;
    private void Awake()
    {
         bodyRef = this;
    }
    public void MenuEnable()
    {
         //... SomeCode
     }
}

Now 'BodyModController.bodyRef' results in Null. I have no idea why. Also, other scripts access 'MenuController menuControllerRef' in Start() or OnEnable() so changing execution order did not help much as my errors count increased to 5. Any help is greatly appreciated.

P.S. MenuController is attached to GameObject at root(level 1) and active, whereas BodyModController is at 4th child level if seen in the hierarchy and is setActive(false) to all its parents and grandparents. Not sure what is terminology but I hope the last point was helpful if relevant.

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 JVene · Jul 23, 2018 at 12:04 PM

My own observation is that child objects are "Started" or "Awaken" before parents in default layout (without using the editor to control script execution order).

I sense this code isn't posted from a compiled example (Start without void wouldn't compile).

private void Awake() is probably fine, but I always see that void Awake() (where private is just assumed) - my thought here is to be sure that Awake is actually executing, try a breakpoint to witness that.

Anyway, the initialization you're invoking in Awake or Start to initialize the static bodyRef may not fit into the timing design of Awake or Start, and as a concept isn't a Unity framework notion, but a more general object oriented design notion, independent of Unity.

Try moving that static member initialization to a default constructor for BodyModController, which should fire the moment the object is created, before an Awake could even be called.

Note, too, there is nothing enforcing singleton behavior here - so, some thought to that might be warranted.

Another method you could consider is to make the static bodyRef private, and insist on the use of the static through a public static method that performs lazy initialization. Any caller that witnesses the bodyRef is null would "Find" the object by name, initialize the static bodyRef, then (in all cases) return it. This is a one time self initialization on first use.

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 TheGameLearner · Aug 01, 2018 at 10:04 AM 0
Share

Sorry for the delay, I ended up creating public reference and calling functions from it. After using public variables, unity did not give any errors. Sorry for not checking your reply earlier but as per the text I believe it will work had I tried it.

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

151 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

null reference exception help 1 Answer

unable to pass parameters to a C# script (no GameObject) 1 Answer

Getting array from Json suddenly doesnt work. 1 Answer

NullReferenceException, Script Error 1 Answer

GetComponent returns null 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