How does Unity know we are looking for the transform of the Waypoints

Hello! I have trouble understanding the following piece of code:

 public List<Transform> GetWaypoints()
    {
        var waveWaypoints = new List<Transform>();
        foreach (Transform child in pathPrefab.transform)
        {
            waveWaypoints.Add(child);
        } 
        return waveWaypoints;        
    }

My question is: How does unity understand that we are looking for the transform of the waypoints and not the transform of the Path, which we have currently set at 0,0,0?

In other words, how does Unity understand that the variable named ā€œchildā€ refers to the waypoints within the prefab?

Is it because we are creating a list, so unity is looking for a list of similar things with a transform? Is it because we say ā€œin pathPrefabā€ and that suggests we are looking for the transform, NOT of the pathPrefab itself, but something within the Prefab (so the waypoints)? Is it something else entirely?

Thanks in advance!

I do not know the project which you are talking about. But looking at your code, Iā€™d say it is because you are adding all children (of type Transform) of PathPrefab.Transform. So the waypoints transforms must be children of Path prefabs transform. You could replace the word ā€˜childā€™ int he foreach statement with ā€˜bananasā€™ or ā€˜cā€™ or ā€˜jiggliesā€™. It wouldnt matter. But I would expect the Waypoint transforms MUST be children of PathPrefabs transform for this code to work.

The coder has used the name child, to make it very easy to see what is happening in the loop.

That clears things up. Thank you!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms