Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
6
Question by MidnightStudiosInc · Jun 11, 2015 at 01:47 AM · objectdestroynull

Is it possible to detect if an Object is *being* destroyed?

It is easy to check if a UnityEngine.Object has been destroyed by checking it for null. But, because Destroy is by default not immediate (and the documentation recommends against using immediate for some reason) there is a brief window until the next frame where an object is on death row but obj == null returns true.

Is it possible to check whether Destroy has been called on an object but it is not yet destroyed?

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
3

Answer by crohr · Jun 11, 2015 at 03:41 AM

The Unity method OnDestroy will get called when the object has had Destroy called on it.

 void OnDestroy()
 {
    // Do whatever
 }
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 MidnightStudiosInc · Jun 11, 2015 at 02:11 PM 0
Share

It would not be possible to detect whether an inbuilt Object types are being destroyed with this method. I'm looking for a general purpose solution.

avatar image crohr · Jun 11, 2015 at 03:50 PM 0
Share

I you are try to check for when a specific Component is destroyed, then the answer is no, you can't deter$$anonymous$$e that a Component is in the process of being destroyed.

Could you tell us more about what specifically you are trying to accomplish, maybe there is another solution that could help you?

avatar image MidnightStudiosInc · Jun 11, 2015 at 04:25 PM 0
Share

I am creating a general purpose Unity Object serializer. It's better not to serialize objects that are being destroyed.

avatar image
0

Answer by maccabbe · Jun 11, 2015 at 04:05 AM

I'm not aware of anything built into unity that would work like

 if(gameObject.isDestroyed)

However, it is possible to mark the gameObject as about to be destroyed yourself. For instance rename the gameObject or change the tag, i.e.

 gameObject.name="ToDestroy";
 GameObject.Destroy(gammeObject);

then you can check however you have decided to mark your gameobject

 if(gameObject!=null&&gameObject.name!="ToDestroy){


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 MidnightStudiosInc · Jun 11, 2015 at 02:13 PM 0
Share

I am looking for a general solution for all subclasses of UnityEngine.Object. This would not work for example in the case of components.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Passing information error 0 Answers

Getting all Object to go next scene. 1 Answer

if object is destroyed 2 Answers

How to destroy a unit contained in multiple lists 1 Answer

What is the reason for this NullReferenceException? 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