Hey guys,
I’m having a little trouble understanding the example in the “Finding Child Objects” section of this page http://docs.unity3d.com/Manual/ControllingGameObjectsComponents.html
public class WaypointManager : MonoBehaviour {
public Transform[] waypoints;
void Start() {
waypoints = new Transform[transform.childCount];
int i = 0;
foreach (Transform t in transform) {
waypoints[i++] = t;
}
}
}
I understand that waypoints is an array of Transform components. I also get that the length of the array is initialised to the number of child objects under the parent.
So far, so good.
Unfortunately, there are two separate elements that I can’t wrap my head around:
PROBLEM 1
I don’t understand the use of transform in foreach(Transform t in transform).
In my mind, transform refers to the Transform Component of the parent object (i.e. the object that the script is attached to). Given that a foreach statement is used to iterate through an array, I can’t understand exactly what’s happening here.
PROBLEM 2
The article states that “The child objects can be retreived using the parent’s Transform Component (since all GameObjects implicitly have a Transform)”.
I don’t understand how I’m getting access the child objects using this approach. At best (if I understood Problem 1), I can see how I might be able to access their Transform components, but I don’t see anywhere where I’m getting access to GameObjects.
Any help that anyone can provide would be greatly appreciated.
Thanks in advance.
Matt