- Home /
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.
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.
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.
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
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
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.
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.
Your answer
Follow this Question
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