Solved: Why transform in foreach loop?

Why are they using transform in this case and not GameObject?
I assume they can use Transform because GameObject is inherited from transform, but why do they do that?

public List<Transform> GetWayPoints() 
{
    var waveWayPoints = new List<Transform>();

    foreach (Transform child in pathPrefab.transform)
    {
        waveWayPoints.Add(child);
    }
    
    return waveWayPoints; 
}

Hi Drafixe,

Welcome to our community! :slight_smile:

Have you already read the description of the Transform class in the API? If so, you know that the Transform object contains information about the child Transform objects. And this functionality is what we are using here. Does that make sense?

Okay, do I understand it right. The for loop goes up to the parent object of the pathPrefab (transform) and look up in a list stored here each Transform child. And then we add every of this childs to our list waveWayPoints in every loop? And the reason we goes up to Transform is that it is the only place the reference to our childs are stored. Is it right?
Sorry my misspellings, I rarely write English. English is not my native language.

The loop iterates over whatever Transform object is assigned to pathPrefab.

[…] and look up in a list stored here each Transform child. And then we add every of this childs to our list waveWayPoints in every loop? And the reason we goes up to Transform is that it is the only place the reference to our childs are stored. Is it right?

That’s right. :slight_smile:


See also:

Thanks for reply. It made the whole thing more understandable.

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

Privacy & Terms