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 Andy-Block · Jan 25, 2013 at 12:09 AM · destroynull

Destroying an object not working

I'm trying to destroy some objects as follows:

     public void ClearAllEditableObjects() {
         foreach (GameObject obj in GameObject.FindGameObjectsWithTag("EditableObject")) {
             print ("ClearAllEditableObjects destroying object (id " + 
                 obj.GetInstanceID() + ") with " + 
                 ((obj.rigidbody == null) ? " no rigidbody" : " a rigidbody"));
 
             Destroy(obj);
             print ("ClearAllEditableObjects testing object (id " +
                 obj.GetInstanceID() + ")  null test yields " + (obj == null));
         }
     }

The text output is:

 ClearAllEditableObjects destroying object (id 10262) with  a rigidbody
 ClearAllEditableObjects testing object (id 10262)  null test yields False

My understanding is that Destroy(obj) will not destroy the object immediately, but will do so before the next frame; and that it will test as being equal to null until that happens. Any clue why this doesn't seem to happen here? Another script is trying to use the object later (it is being returned from a Physics.OverlapSphere() call), which then causes problems. Any thoughts gratefully received! Regards,

  Andy
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 · Jan 25, 2013 at 12:10 AM

It starts returning null after it has been destroyed at the end of the frame, until then it appears valid. Use DestroyImmediate to delete immediately.

Comment
Add comment · Show 7 · 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 Andy-Block · Jan 25, 2013 at 11:53 AM 0
Share

I considered that option, but the warnings in the documentation seemed to strongly suggest that was not the best idea: "This function should only be used when writing editor code since the delayed destruction will never be invoked in edit mode. In game code it is recommended to use Object.Destroy ins$$anonymous$$d. Destroy is always delayed (but executed within the same frame) Use this function with care since it can destroy assets permanently!". Is there no other way to tell if an object is scheduled for deletion?

avatar image Wolfram · Jan 25, 2013 at 12:08 PM 0
Share

In that case, simply clear the reference after the Destroy() manually:

 obj=null;
avatar image Andy-Block · Jan 25, 2013 at 12:27 PM 0
Share

But in the problematic code, it is not being accessed through the same reference; it is being returned by Physics.OverlapSphere().

avatar image Wolfram · Jan 25, 2013 at 12:35 PM 0
Share

Personally, I'd still use DestroyImmediate. A workaround could be to deactivate the object for the rest of the frame. So ins$$anonymous$$d of my suggestion above (which didn't make much sense anyway, since "obj" was a local variable), use

 obj.SetActive(false);
avatar image whydoidoit · Jan 25, 2013 at 12:55 PM 1
Share

The reason that they suggest that is that a variable with a valid reference in Update may be invalid by LateUpdate (or a coroutine etc) - I'd use DestroyImmediate.

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

Destroyed gameobject become Null ? 1 Answer

if ( Transform == null) ?? Empty Transform reference 1 Answer

missing gameobject ≠ Null? 1 Answer

Destroyed Object Stops Scipt 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 2 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