• 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
5
Question by Joe Robins 1 · Jan 12, 2010 at 02:01 PM · gameobject

Does Unity null all references to a GameObject after Object.Destroy is called on it? If so, how?

So when you Destroy a game object.. any and all references to it turn null. How exactly does Unity implement this functionality? Is it a built-in .Net feature that anyone can use, or is it just a reference registry in each game object somehow?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by Joe Robins 1 · Jan 12, 2010 at 02:01 PM

To quote Lucas Meijer:

It's a special thing we implemented (and which is only turned on in the editor). When you Object.Destroy() a unity object, the managed wrapper (the .net object you usually interact with), is not thrown away. instead the managed wrapper realizes that the object it wraps is destroyed, and informs you of that fact when you try to do anything with it.

When you have no more references to the .net object, and a new garbage collector run happens, the actual wrapper object itself will be cleaned up.

Comment
Add comment · Show 3 · 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 burnumd · Jan 12, 2010 at 02:58 PM 1
Share

It might be nice to cite Lucas $$anonymous$$eijer for providing the answer in this thread: http://forum.unity3d.com/viewtopic.php?t=40549

avatar image vargonian · Mar 31, 2012 at 08:30 AM 0
Share

Wow, I spent over an hour trying to figure out why this was happening. In my case it's undesirable behavior, but I can work around it so it's alright.

avatar image Zergling103 · Oct 17, 2013 at 01:56 AM 2
Share

Spooky. And Evil.

avatar image
-1

Answer by Fernando Zapata · Mar 23, 2010 at 06:59 PM

I just tested this but got the same behavior in the web player as in the editor.

I created two Spheres. One sphere had the following script:

 function OnMouseDown() {
   Destroy(gameObject);
 }

The other sphere had the this script:

 var go : GameObject; // set this to the other sphere
 
 var doneOnce = false;
 function Update() {
   if (go == null && !doneOnce) {
     doneOnce = true;
     GameLog.GetInstance().Info("go == null"); // my game log GUI
   }
 }

In both cases the "go" reference was set to null automatically when I clicked on the sphere that "go" referred to.

What's the expected behavior from Unity? Is this a bug or a feature working as expected?

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.

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

1 Person is following this question.

avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

How to destroy object forever? when i go to another scene and back to first, i want to that object stay destroyed! , Please !!!!  1 Answer

Access Transform of an Array of GameObjects 1 Answer

How can I add the OnTriggerEnter function to all game objects that I instantiate? 1 Answer

Spawning random objects. Trouble /w objects. Null Reference. 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