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
3
Question by chillypacman · Apr 14, 2013 at 02:24 PM · c#objectmemorypool

Override Destroy function?

I'd like to be able to make certain gameobjects not get destroyed when Destroy(gameObject) is called on them.

Is there a way to make this happen such as overriding the 'OnDestroy' function? I ask because I'm trying to create a gameobject pool.

Comment
Add comment · Show 1
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 BeerPlusPlus · Dec 17, 2017 at 12:54 AM 0
Share

I was thinking the same thing. If I figure it out, I'll post.

P.S. Love the Scorpio pic. Worth an up-vote right there.

5 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Bunny83 · Dec 17, 2017 at 03:22 AM

No, you can't. The Destroy method is a static method. In order to override a method it needs to be a virtual instance method.


The OnDestroy callback is a callback which has no influence on the actual action. It's just a notification that this object got destroyed. If you don't want to destroy the object but return it to a pool, just don't call Destroy and instead return it to the pool.

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 bburtson09 · Dec 20, 2017 at 04:34 AM

You should in theory be able to use member hiding. I havent tested but

 //define member same signature woth new keyword
 new void Destroy(UnityEngine.Object obj) 
 {
      //no-op
 } 


 

@chillypacman let me know of this works super curious

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 Pangamini · Dec 20, 2017 at 09:43 AM

when you call Destroy(Object), it's too late to try and save it. What I usually do is that I have a custom non-static virtual method Destroy on my monoBehaviour base class (with an extra onDestroy event for others to hook to). This method is usually implemented as Destroy(gameObject), but may vary for different objects. If you were using this method instead of static Object.Destroy(Object), you might be able to implement some pooling there. That might still mean a lot of rewriting, but should be quite straightforwad

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 cos_ninja · Oct 01, 2019 at 08:24 AM

The logic behind not being able to override the Destroy() method is because overriding it is a bad coding habit. You don't want to mix in anything else into it. Always think of the army of other developers working with your code afterwards. Think how much time they'd loose if they'd have to figure out that Destroying the object doesn't always mean that it's destroyed.

Put it in the pool instead of calling Destroy(), and leave Destroy purely for destruction.

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 cmersereau · Dec 04, 2020 at 06:26 PM

I do not have exhaustive knowledge on the subject, but it is possible to prevent object destruction in certain situations. I am working on an app that has a singleton that we want to persist scene changes, and there is a method in the Object class DontDestroyOnLoad(Object objectYouDoNotWantDestroyed); which will allow the object to persist. This isn't a direct answer to the question, as it isn't changing the behavior of the Destroy() function, it is I believe simply bypassing the call happening. It may help someone looking for this functionality though.

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

17 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Best way to load assets from AssetBundle? 1 Answer

ARCore Anchor 3D object 0 Answers

C# cast object to gameobject not working 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