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
0
Question by Asse1 · Nov 24, 2012 at 02:31 PM · gameobjectnull

GameObject member becomes null for no reason

I have a struct (could also be a class, doesn't change anything) which has a public GameObject member. This public member sometimes somehow gets null, completely on it's own. I also made the member private and wrote a getter, setter to check if somewhere it is set to null but that's not the case.

Also what's intereseting is, that printing this member with Debug.Log prints "Null" when I set it to null on my own and it prints "null" when it turns null automatically.

So this is the C# struct with getter and setter where I can check if the private member gameObject gets set to null, but that's not the case, it just turns null sometime on its own:

 private struct Helper
 {
     private GameObject gameObject;
 
     public GameObject GameObject
     {
         get { return this.gameObject; }
         set { this.gameObject = value; }
     }
 }

Thanks for any help!

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
0

Answer by whydoidoit · Nov 24, 2012 at 02:32 PM

Unity automatically makes GameObject references (and all Components) turn null when ever you destroy the game object. You can't stop that from happening - so you'll have to work out where it gets destroyed.

Comment
Add comment · Show 9 · 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 Asse1 · Nov 24, 2012 at 02:47 PM 0
Share

It doesn't get destroyed and when it gets destroy your reference turns invalid, but not null.

avatar image whydoidoit · Nov 24, 2012 at 04:43 PM 0
Share

No - they turn null. That's how Unity works.

If they are null, something has destroyed that object - like loading a level etc.

avatar image whydoidoit · Nov 24, 2012 at 04:47 PM 0
Share

In fact it can be very annoying, because you get exactly the effect you are seeing. Unity makes anything that has Destroy called on it return null - in the same way as they made Unity object references castable to bool.

So you can do

   if(someComponentReference) ...

But cannot do

    if(someNormalClassReference) ...
avatar image whydoidoit · Nov 24, 2012 at 04:58 PM 0
Share

See this thread where the Unity tech master explains the functionality.

avatar image Asse1 · Nov 24, 2012 at 08:04 PM 0
Share

Hm okay, then maybe something really gets destroyed.. thing is I still can see the GameObject inside the game. I'll see if it's a logical problem. Thanks for the help!

Show more comments

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

12 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

Related Questions

how to check for GameObject is null in array with random 1 Answer

Instantiate gameObject returns null? 1 Answer

How to access gameobject array set in inspector? 2 Answers

Null Reference Problem Code+pic (SOLVED) 2 Answers

Simple Looping drag and drop game 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