• 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 MihailGerogescu · Jul 15, 2016 at 12:12 PM · nullreferenceexception

GameObject - missing reference - strange behaviour

Ok guys. Here's the issue. Here's my project setup. Starting the game in the MainMenu , from there loading a new scene, going to the GameScene, all good all fine, everything works good, end game conditions are met, the MainMenu scene is loaded again, from there AGAIN i go to a new game. From here things start to act wierd. I have a GameObject called "X" let's say, that GameObject has a child called "Y", in the Start() - "X" get's a reference of "Y" using GetChild(), in Debug mode everything looks good, no null reference, the object loaded without a problem, nothing, but when an event handler in "X" tries to access "Y" says that the object is destroyed and that the reference is missing which is wierd. As I said , first run of the game, no exception is thrown, but the second run this is the behaviour I get. The game works good even after the exception! The event handler enables and disables the gameObject that throws an exception without a problem! But why that exception? Am I missing something related to Scenes? Thank you in advance, appreciate.

Comment
Add comment · Show 2
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 _Yash_ · Jul 15, 2016 at 01:21 PM 0
Share

can you show code which affects X and Y?

avatar image MihailGerogescu _Yash_ · Jul 15, 2016 at 02:07 PM 0
Share
 public class x
 {
     GameObject Y;
     
     void Start()
     {
           Y = transform.GetChild(1).gameObject; **//Checking this in debug mode , I can tell that the object loaded correctly, no null reference at this point. Between Start() and EventHandler call NOTHING affects that OBJECT**
           Game$$anonymous$$ananger.Instance.GetPlayer(_side).SomeEvent += EventHandler;
     }
     
     void EventHandler()
     {
          Y.SetActive(false); **//THIS IS WHERE THE EXCEPTION IS THROWN - hovering over the Y in debug mode says that the reference is null**
     }
     
  }

It's something like this. First time loading the scene everything works great. The second time throws the exception. That the reference is missing. I will try to look deeper into it. It might be some static data and did not change between loading the scenes.

1 Reply

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

Answer by Dave-Carlile · Jul 15, 2016 at 02:38 PM

 GameMananger.Instance.GetPlayer(_side).SomeEvent += EventHandler;

Where are you removing the event registration from SomeEvent? I assume that your GameManager never gets destroyed when you're switching scenes. This means it continues to hold a reference to that EventHandler method. When the event gets triggered it tries to call all of the registered handlers. When it gets to this object (which has been destroyed as far as Unity is concerned, because of the scene reload) you're getting the error.

So you need to add an OnDestroy method so you can remove the event handler when the object is destroyed.

 void OnDestroy()
 {
     GameMananger.Instance.GetPlayer(_side).SomeEvent -= EventHandler;    
 }


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 MihailGerogescu · Jul 15, 2016 at 03:00 PM 0
Share

Dave! Yes I missed that. And you know why I missed that? Yes the Game$$anonymous$$anager is an object that does not get destroyed between scenes as it passes data between scenes. I did one thing and I thought it would work. Since Game$$anonymous$$anager is a singleton and set it so that is not destroyed between game scenes. I was manually destoying the Game$$anonymous$$anager when going from GameScene to $$anonymous$$ain$$anonymous$$enu scene. This way I was wiping all possible data. Well atleast that's what I thought. Unsubscribing OnDestroy in that object worked. I totally missed that. Thank you Dave.

avatar image iancox890 · Dec 06, 2018 at 06:15 PM 0
Share

Thanks so much! Had this problem too and you completely nailed the cause.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Material[] Object reference not set when instantiating 2 Answers

NullReference Exception when adding a class 2 Answers

NullReferenceException 1 Answer

Raycasthit NullRException 1 Answer

Error in Build but not in Editor - output_log.txt debug problem 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