Question by
bertadevant · Feb 08, 2016 at 12:43 PM ·
prefabtutorialchildrenrandomizingout of bounds
Random Movement of children of prefab
Hi I am starting with some Unity tutorials, I am fairly ok with C# scripts knowing C++ but completely new to the actual Unity environment. I was following this tutorial to create a 2D UFO and when I finished I though it would be fun to try and see if I could make the Pikcup elements move at random and the UFO has to chase them.
So I built this:
for (int i=0; i < transform.childCount; i++)
{
pickup = transform.GetChild(0).gameObject;
if (goneHorizontal == false)
{
pickup.transform.Translate(new Vector3 (0,Random.Range (-14f,14f)+y, 0));
goneHorizontal = true;
} else {
pickup.transform.Translate(new Vector3 (Random.Range (-14f, 14f)+ x, 0, 0));
goneHorizontal = false;
}
}
As you might imagine this class is inside a prefab called Pickup that has 12 other children (following how the tutorial tells you to built it). The problem is that when I try to do (Debug.log(transform.childCount)) to check why I was getting a Transform Child out of bounds I realized that the Parent has no child. Why is it not recognizing my objects?
Thanks for any help!
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612063149im_/https://answers.unity.com/themes/thub/images/avi.jpg)