Run code on GameObject's destroy

I’m having a problem where I need to keep a list of certain GameObjects in an object with their id and reference to the GameObject which I call entity in code. I want to use this object to get the specific entities when I want to run some action on them from my React ui code.

This means that I need to make sure that my entitiesStore is up to date with existing GameObjects in the game. While it is easy enough to add them to the store for example in constructor of the entity, I’m not sure how to remove them from my store when it is required. Is there any way to call some code before destroy() in the GameObjects?

I’m aware I could use this.children.getByName('string-name'); etc. to get GameObject by its name but I need to fetch objects in code where the current Scene is not readily available or when I can’t be sure which Scene contains the wanted GameObject.

As a bonus, is there property in GameObject which I could check to know if the GameObject has been destroyed?

You can put a preDestroy function on your game objects, either as a method if you’re using a custom class, or by assigning it to an existing game object (gameObject.preDestroy = function...).

Thanks, didn’t find that from docs since it does not show in GameObject or Sprite’s list of methods where I expected it to be :thinking:

You can use the DESTROY event for this.

After destruction a Game Object’s scene property is undefined.