- Home /
Destroying of parent causes issues for OnDestroy() in children
I'm working on an editor script that makes some nodes and gives them neighbors. This works fine. What I'm implementing is a system where you can destroy a node from the editor window, and it removes itself from neighbors' lists, so as to avoid null list values. The code is as follows:
public void OnDestroy ()
{
if (neighbors != null)
{
foreach (Node node in neighbors)
{
node.neighbors.Remove(this);
}
}
}
This functions properly when the child is directly deleted, but upon deleting a parent node containing a multitude of nodes, it isn't properly done, leaving lots of null list values in neighbors. I've tried having it check if it's a parent and recursively calling a secondary purging function on its children, but the results are the same every time.
This SHOULD work in practice, as destroying a parent calls OnDestroy() on every child. Any ideas what might be the cause?
Also, secondary, less important question: when debugging, I notice OnDestroy() is called twice on every object when deleted in editor mode, any idea why that is?
Thanks!
Same issue here.. did you find a solution for this? Seems like OnDestroy is not properly called in children.