- Home /
How to add default behavior of OnDestroy function for all MonoBehavior scripts?
Hello, I want all monobehaviors to unsubscribe from events in OnDestroy call without even implementing it, but I'm not sure how to do this. I was thinking that I could implement extension method of MonoBehavior and add OnDestroy function and default behavior to it, than when object inherits from MonoBehavior it would by default run OnDestroy when gameobject is destroyed, but I'm not sure if that's correct approach and if it can be done that way. I Just want to avoid having OnDestroy written and implemented in same way in all MonoBehaviors, that looks awful.
Answer by xxmariofer · Mar 04, 2019 at 08:31 AM
something like this is what yoou are looking for
public class OnDestroyMonobehaviour : MonoBehaviour
{
protected void OnDestroy()
{
//your stuff
}
}
public class RandomClass : OnDestroyMonobehaviour
{
private void Start()
{
//you can use it as a normal monobehaviour method and will implement the OnDestroy from the parent
}
}
Your answer
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
Serializable class with coroutines? 1 Answer
Working with Items and Class Inheritance for an RPG Item Database 1 Answer
Is it possible to create game object/prefab from file? 2 Answers
Inheritance in Unity: Gameobject references runtime script in scene file. Fixing! 0 Answers