Help me understand "Transform child in transform"

Sometimes I like to understand code clearly before I move on in the course. Right now I am stumped as I don’t understand the roles “Transform” and “transform” are playing under this code:

foreach(Transform child in transform)
        GameObject enemy = Instantiate(enemyPrefab, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
        enemy.transform.parent = transform;

I understand Transform is related to Position, Rotation, and Scale, but what exactly does “Transform child in transform” mean?

Thank You!.

Hi @Pixelrave,

The foreach loop is comparable to a for loop but it iterates over a collection without any indices. Please take a look at the examples on DotNetPearls to get a better idea of the enumeration statements and what a foreach loop is good for.

You can read it as:

foreach (Type variableName in variableNameForCollection that contains items of type Type)

child is an arbitrary variable name. You can name the variable whatever you want whereas transform is a predefined variable in the MonoBehaviour class our script inherits from. See : MonoBehaviour next to the class name. We did not create transform, we just use it.

In the API, you can find more information on the Transform class. A Transform contains information about the child objects and the parent in Unity.

Did this help?

1 Like

Hello Nina,
Thank you for explaining with detail what the code does. So, what’s the difference between Transform and transform? I understand that Transform changes position, rotation, and scale. I just need to be explained what
"foreach(Transform child in transform)" is supposed to do as if I were a 5 year old kid lol (just the code inside parentheses).

I understand the code between brackets to initialize instancing though.

Oh no, my answer’s gone. :frowning:

So here’s a brief version:

a) Transform is a non-static class and it needs instances which can be used. A non-static class is like the blueprint of a car, and its instances are like the actual cars. See value types vs. reference types. Since C# is a language, you need to name stuff. Variables of reference types like Transform contain a “link” to an existing instance, or they are null.

b) Instances of the Transform class don’t change anything. They just contain information like position, rotation, scale, parent and more. See the API. You can assign new values or references to those variables.

c) The transform variable in the foreach enumeration statement contains a reference to an existing Transform instance, see a) and MonoBehaviour. Transform child is a local variable that exists within the scope of the foreach block to allow you to do something with the single elements transform contains.

1 Like

Hello Nina,
I appreciate the help you have given me. Your answers made me realize that I had more to understand about C# than I actually did, so I went back to C# (without Unity) to learn more in depth about it’s rules and such and after doing so, I could understand your explanation much better.
Thank you!.


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

Privacy & Terms