I have a spell system where a the spell is a self-contained game object handling all the logic. When done, it destroys itself. This works great
Then I added a second game object handling animation, and bundled it under a parent object. The spell still destroys itself fine, and the animation object destroys itself after the animation, great. That leaves me with the parent which should destroy itself when all children are done and gone.
The trouble is that the child objects have no information about each other, nor do they destroy themselves in any fixed order. (The player has the option to decide spell-by-spell if they want sound and/ or animations to be enabled; sometimes there is only one child, sometimes 3).
While I could check in the parent in each Update if there are children, it seems like that would add too much overhead with lots of spells for a check that is most of the time not useful.
Informing the parent via UnityEvent like this (executed on the child)...
public UnityEvent OnCleanupDone; public void DestroyEverything() { Destroy(gameObject); OnCleanupDone?.Invoke(); } ...is also not working, since when the parent subscription function is called, the child is still alive.
How can I handle this in a robust and efficient way?