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
5
Question by davidevolta98 · Dec 07, 2016 at 07:22 PM · c#unity5inspectornullpublic variable

GameObject fields evaluate to null, but they are actually assigned and working

I've been browsing for days on the internet, desperately trying to solve this problem. Basically, I have a simple game manager script in my game which needs references to game objects that are part of the UI. I have called them UITutorialText, UITutorialImage, UIGamePaused and they are, respectively, a Text which shows up as a tutorial, an image for the tutorial and a text which gets displayed when the game is paused. I assign them through drag and drop in the inspector, but whenever I check their value in the script they evaluate to null, even though the script is actually working and I never get any NullReference error. This happens in the Awake, Start or Update method in the same way. Here are the relevant bits of code (I omitted parts which do not use those fields):

 [...]
 // UI elements to control
     public GameObject UITutorialText;
     public GameObject UITutorialImage;
     public GameObject UIGamePaused;
 [...]
 
 void Awake()
     {
         // setup reference to game manager
         if (gm == null)
             gm = this.GetComponent<NewGameManager>();
         currentSceneName = SceneManager.GetActiveScene().name;
         // setup all the variables, the UI, and provide errors if things not setup properly.
         setupDefaults();
     }
 
 void setupDefaults()
     {
         [...]
         // friendly error messages
         if (UIGamePaused == null)
             Debug.LogError("Need to set UIGamePaused on Game Manager.");
         if (UITutorialText == null)
             Debug.LogError("Need to set UI Tutorial Text on Game Manager.");
         if (UITutorialImage == null)
             Debug.LogError("Need to set UI Tutorial Image on Game Manager.");
 
         [...]
         }

As you can see, variables are given a value in the inspector:

Variables assigned in the inspector

But then I see 3 errors in the console which mean that I have 3 null pointers:

GameObjects are null

What might I be getting wrong? Thanks in advance!

EDIT: I ran some tests and I discovered something very strange: if I log the value of one of the fields (UITutorialText) every time that Update runs, I get null, then a correct value, then null, then a value, then null and so on... It looks like something is changing those variables at every Update cycle, but I only use them in this very script.

unityscreen2.jpg (89.2 kB)
unityscreen1.jpg (48.5 kB)
Comment
Add comment · Show 9
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 UnityCoach · Dec 07, 2016 at 07:39 PM 0
Share

Hum, is your class named NewGame$$anonymous$$anager ?

or is NewGame$$anonymous$$anager a component on the same game object?

I understand gm is a static member to reference a sort of singleton. Also, you don't need to specify this here : gm = this.GetComponent();

 gm = GetComponent<NewGame$$anonymous$$anager>();
 
 // you may prefer to use this
 gm = FindObjectOfType<NewGame$$anonymous$$anager>();
avatar image davidevolta98 UnityCoach · Dec 07, 2016 at 09:29 PM 0
Share

$$anonymous$$y class is named NewGame$$anonymous$$anager and this script file is attached to a GameObject called Game$$anonymous$$anager. Therefore, Game$$anonymous$$anager has a script component named NewGame$$anonymous$$anager.

Yes, it's kind of a "dirty" singleton. I know I don't need to, but I love specifying it anyway, it makes the code look clearer to my eyes... is it bad practice? Co$$anonymous$$g from a Java background, I don't really know.

avatar image UnityCoach davidevolta98 · Dec 07, 2016 at 10:07 PM 1
Share

Ok, got it. Singletons are definitely handy. In Unity, there are different implementations possible, with time, I figured this one was the best fit most of the time.

     private static Game$$anonymous$$anager _instance;
     public static Game$$anonymous$$anager Instance
     {
         get
         {
             if (_instance == null)
             {
                 _instance = (Game$$anonymous$$anager)GameObject.FindObjectOfType(typeof(Game$$anonymous$$anager));
 
                 if (_instance == null)
                 {
                     GameObject go = new GameObject("Game$$anonymous$$anager:Singleton");
                     _instance = go.AddComponent<Game$$anonymous$$anager>();
                 }
             }
             return _instance;
         }
         set
         {
             _instance = value;
         }
     }
 
     void Awake()
     {
         Instance = this;
     }
 
     void OnDisable() // optionally reset instance to null if component is disabled
     {
         Instance = null;
     }
 
     void Destroy() // optionally reset instance to null if object is destroyed
     {
         Instance = null;
     }
Show more comments
Show more comments
avatar image UnityCoach · Dec 09, 2016 at 01:20 PM 0
Share

Here's another little optimisation you can do, which may lead to identify the problem.

When you want to "contraint" a member to a specific type, like say an UI.Text, it's always better to set the member type specifically, ins$$anonymous$$d of just Transform or GameObject. Like

      public Text UITutorialText;
      public Image UITutorialImage;
      public Button UIGamePaused;

Then, if you happen to need access to the GameObject itself, you can always access the .gameObject property.

You can also use stack trace to see what is changing the property.

avatar image davidevolta98 UnityCoach · Dec 09, 2016 at 01:28 PM 0
Share

That is what I was initially doing, I then switched to GameObject thinking it could have been a possible cause of the error and never switched back... I'll change it, thanks for pointing it out.

Thanks for the stack trace suggestion too, I didn't even think about it (how stupid of me): I'll try debugging with it and let you know.

avatar image davidevolta98 UnityCoach · Dec 10, 2016 at 12:44 PM 0
Share

Ok, it looks like the problem is not strictly related to these particular fields: if I put a random new GameObject for testing purposes (public GameObject NullTest), whatever object from the hierarchy I drop in this field behaves in the same way: null on one iteration, correct value in the next one. Like if there were 2 concurrent instances of the same script...

I'll continue investigating... I hope that I'll finally find a solution to post here.

1 Reply

· Add your reply
  • Sort: 
avatar image
9

Answer by davidevolta98 · Dec 12, 2016 at 01:33 PM

Ok, I am so stupid... I just found the solution. It looked like there was another instance of the same script running, so I disabled every game object except for my GameManager and tried to enable back the others one by one... the same script was attached in a Background Camera nested into my Main Camera... removing that fixed everything.

Sorry for the time I have made you uselessly waste.

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 UnityCoach · Dec 14, 2016 at 12:05 AM 1
Share

Hey, no worries, here's a little trick that'll save you time :

in the hierarchy view search field, type "t:nameOfYourClass". It'll filter the view to display the objects using that component.

Cheers

avatar image hexagonius UnityCoach · Feb 08, 2018 at 02:48 PM 0
Share

nameOfYourClass is enough when typed case sensitive

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

15 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

Related Questions

Scale of object does not update properly with public variable 1 Answer

Unity 4.1.5 is not showing certain public variables in the inspector 1 Answer

,Unity Custom Inspector CreateInspectorGUI redraw on change 1 Answer

Public fields not showing in inspector 1 Answer

Setting value in inspector does not update script. 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