- Home /
getchild(0), but first child is gonna be destroyed!
I have this piece of code to acess the transform of the first child:
Transform child_transform = transform.GetChild(0);
but, in some point of the code, this child is destroyed:
Destroy (child_transform.gameObject);
But when its destroyed, unity gives and error saying that the reference was destroyed (and in fact it was).
But the fact is that the parent always have more than one child (in other words, when the first one is destroyed, another one takes the place as the first one), and i wish that my script keep returning the transform of the first child, despite the previous one was destroyed.
How can i do that?
Disclaimer: its a 2d game, flappy bird like
Answer by unity_ek98vnTRplGj8Q · Jan 29, 2020 at 09:22 PM
I suppose you could replace every instance of child_transform
with transform.GetChild(0)
. Not the most efficient, but would get the job done. Otherwise, when you call Destroy() on the child_transform you could then immediately reassign the child_transform to transform.GetChild(0).