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 /
avatar image
1
Question by _RC · Sep 25, 2016 at 07:17 PM · destroydestroy objectnull exception

Clear reference when destroying gameobject

Hi!

 var coolObject : GameObject;
 
 function DestroyObject(){
 Destroy(coolObject);
 }
 
 function TestForObject(){
 if(coolObject == null){
 Debug.Log("It's gone");
 }
 }



After coolObject got destroyed, how do i check if it is still there or not? Check == null gives me false.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by szimmermann · Sep 25, 2016 at 07:28 PM

Destroy() will not change the value of the instance variable you pass to null. This has to be done by yourself.

If you write

 function DestroyObject()
 {
      if( coolObject != null )
      {
         Destroy(coolObject);
         coolObject=null;
         Debug.Log("destroying cool object");
       }
      else
      {
         Debug.Log("It's gone");
      }
  }

you are always on the safe side, even if you are calling DestroyObject() multiple times from different code locations.

Comment
Add comment · Show 1 · 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 _RC · Sep 25, 2016 at 07:41 PM 0
Share

But i am not destroying the object from the same script. I really need a way to check if the object exists or not.

avatar image
1

Answer by AurimasBlazulionis · Sep 25, 2016 at 07:29 PM

This should be null. There are few things to keep in mind. First if you call destroy and then check, it will not work because unity destroys things after the frame, not in between.

Second, the destroyed game object reference works weird, you should try checking if (shootTarget == null || shootTarget.Equals(null)). This seems to solve issues for many.

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
avatar image
0

Answer by meat5000 · Mar 28, 2017 at 05:15 PM

This is how to test for the existence of an object :

  var coolObject : GameObject;
  
  function DestroyObject()
  {
      Destroy(coolObject);
  }
  
  function TestForObject()
  {
      if(coolObject)  // <-- Test occurs here
      {
           Debug.Log("Not gone");
      }
      else Debug.Log("Destroyed");
  }
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
avatar image
0

Answer by shadowpuppet · Mar 28, 2017 at 07:29 PM

I run into this often. I like to destroy things I don't need any more but often have that object referenced in some other script and I hate those red warnings saying I am trying to access something I destroyed more than having unneeded objects in the scene. So I just SetActive (false) or SpriteRenderer.enabled false or some other component of the object disabled once it has served it's purpose. Or if I destroy the object because no longer needed I also destroy the script that references it ( if it too is no longer of use) and if the reference to the destroyed object sits in a larger script and I really want to destroy the object I will put that reference in a different script

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.

Follow this Question

Answers Answers and Comments

60 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 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 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 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

Prevent destruction of an object's child 1 Answer

Collision Detection not working? 1 Answer

Spawned objects randomly not destroyed 1 Answer

Deleting objects from list sequence 1 Answer

Destroying Object without removing attached prefab, How can I destroy an object without destroying the original prefab attached? 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