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.

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.

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) 

@chillypacman let me know of this works super curious

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

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.

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.