I have a gameObject that can be destroy by multiple entity in my game, and i would like to know if there is a way to know which script called the OnDestroy function of the object?
My idea would be to create an event that will register which object to destroy and the gameObject calling it but it would be redoing the Destroy() function. So maybe there is an easier way to do so.
On your target gamobject (the one you destroy), add a script (let’s call it TargetDestruction for this exampe) which will take care of the desctruction, forwhich you have to pass a GameObject parameter.
public void CallDestruction(GameObject objectThatCalledDestruction)
{
// Do your stuff (add to list, destroy, etc..)
ExampleManager.Instance.myListofObjects.Add(this.gameObject, objectThatCalledDestruction)
}
So instead of calling “Destroy(target)” from the caller, simply call target.GetComponent().CallDestruction(this.gameObject);