How to cycle through children?

I am making a script that starts an animation at a random spot in the animation cycle. My current Code is:

function Start () 
{ 
   animation ["Take 001"].time = Random.Range (0, animation ["Take 001"].length); 
   animation ["Take 001"].speed = Random.Range (0.21, 1.01);
   animation.Play ("Take 001"); 
}

My problem is that I have ~30 of these objects and am still tweaking them in Maya. So I don’t want to break the prefab by attaching a script. So my solution would be to make an empty game object and make all of the prefabs children of this object then attach a script to that empty game object and apply the script to each child individually.

So how to I cycle through the children and apply the script to each one so they all get different values?

You can just do “apply changes to prefab” after you break the connection.

–Eric

You can use GameObject.GetComponentInChildren(Animation) with a for loop. I had to do this myself recently to add – at runtime – a rigidbody component, convex mesh collider, and apply a random rotation and velocity to about 30 pieces of debris that I had thrown into an Empty, so I wouldn’t have to faff about with defining new prefabs.