Code works, but why does this work?

So everything is working but I don’t understand how this bit is actually working:

foreach (Transform child in transform)
{
    child.gameObject.SetActive(shouldShow)
}

How does the foreach loop know you want to iterate over the children in the Transform? How does it know that transform is not just an object?

1 Like

The Transform type implements the IEnumerable interface. This means the Transform also acts like a List or Array of child transforms.

1 Like

That is good to know. Thank you very much for the explanation.

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

Privacy & Terms