Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
7
Question by Piflik · Sep 23, 2011 at 08:10 AM · javascriptinstantiateondestroy

Instantiate OnDestroy

I have a couple of objects that spawn a particle effect, when they are destroyed. Everytime I start the game from within Unity and then stop it, each object I have not destroyed during runtime spawns his death-effect and these linger in my hierarchy. Is there a way to prevent this from happening?

Script: function OnDestroy() { Instantiate(object, position, rotation); }

Comment
Add comment · Show 4
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 syclamoth · Sep 23, 2011 at 08:31 AM 1
Share

The problem is that OnDestroy gets called when the objects get cleaned up when you go back to the editor! If you don't want this to happen, you will have to spawn your death effects another way.

avatar image Piflik · Sep 23, 2011 at 08:57 AM 0
Share

Feared as much...luckily it is no big deal to pull the Instantiate out of OnDestroy and put it right before I destroy the object itself.

avatar image Bunny83 · Sep 23, 2011 at 01:33 PM 0
Share

That sounds like you use ExecuteInEdit$$anonymous$$ode... Are you sure that you need the scripts functionality at edit time? You have to be careful with ExecuteInEdit$$anonymous$$ode, it can break your whole scene when used wrong. Only use it when you really need it and you know what you're doing...

avatar image Piflik · Sep 23, 2011 at 02:11 PM 0
Share

No no..it is as syclamoth said. When I end playmode, all objects get cleared and call their OnDestroy function. I didn't even know about ExecuteInEdit$$anonymous$$ode until now :D

1 Reply

· Add your reply
  • Sort: 
avatar image
22
Best Answer

Answer by Tommynator · Sep 23, 2011 at 09:28 AM

Make use of the fact that on each MonoBehaviour OnApplicationQuit() is called before OnDestroy():

private bool isQuitting = false;

 void OnApplicationQuit()
 {
     isQuitting = true;
 }

 void OnDestroy()
 {
     if (!isQuitting)
     {
         //your code
     }
 }

Cheers, Tommy

Comment
Add comment · Show 8 · 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 Piflik · Sep 23, 2011 at 09:43 AM 0
Share

Thanks. That works.

avatar image syclamoth · Sep 23, 2011 at 10:11 AM 0
Share

Ohh, good one!

avatar image Aily · Jul 11, 2014 at 12:38 PM 0
Share

$$anonymous$$uch thanks ;)

avatar image hirti · Jul 12, 2014 at 05:15 PM 0
Share

thanks for the simple answer :)

avatar image noio · Jun 15, 2015 at 09:53 AM 0
Share

The diagram at the bottom here seems to suggest the opposite: http://docs.unity3d.com/$$anonymous$$anual/ExecutionOrder.html

It shows OnDestroy is before OnApplicationQuit. Is it wrong?

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Automatically load asset bundles on start from cache 0 Answers

How to fix Error BCE0043: Unexpected token: Network on Network.js for network script 0 Answers

Dreaded UCE0001 ';' error 2 Answers

Unity Frame Drop From Instantiating Prefab that has script 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