8
\$\begingroup\$

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?

\$\endgroup\$
5
  • \$\begingroup\$ If spells are used frequently, a robust and efficient solution would be to use a pooling system, rather than instantiating and destroying objects each time a spell is cast. \$\endgroup\$ Commented 16 hours ago
  • 1
    \$\begingroup\$ The natur of the spell itself makes pooling not a good solution. A spell can be modified a lot during the cast with added components, it would be more tricky to remove everything to a clean state for the pool. \$\endgroup\$ Commented 15 hours ago
  • 1
    \$\begingroup\$ This is one of those programming questions that's fun to show to non-programmers with no context :D \$\endgroup\$ Commented 12 hours ago
  • 1
    \$\begingroup\$ Are you sure your question shouldn't go to parenting.stackexchange.com or interpersonal.stackexchange.com instead? \$\endgroup\$ Commented 4 hours ago
  • \$\begingroup\$ Only if I used a catchy title like: After killing all the children, how do I dispose of their parent afterwards? \$\endgroup\$ Commented 4 hours ago

1 Answer 1

8
\$\begingroup\$

After searching for a better way, I came across OnTransformChildrenChanged:

The following changes to direct children of a GameObject invoke OnTransformChildrenChanged:

  • Adding a new child object to the GameObject's Transform.
  • Removing a child object.
  • Moving a child to a different parent.
  • Changing the order of the direct children.

It makes no difference whether these actions are performed in the Hierarchy window or from code with APIs such as Transform.SetParent, Object.Destroy, or Transform.SetSiblingIndex.

OnTransformChildrenChanged is only triggered by changes to the immediate (direct) children of the Transform. Changes to grandchildren or deeper descendants do not invoke this method on the parent.

Adding this event to the parent now requires only a check against the child count to know if they all got removed or not.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.