newAttacker.transform.parent = transform;

Hey rick,

Small question, I couldn’t exactly understand this line of code newAttacker.transform.parent = transform;

Is it the same way as saying “The parent of the attackers’ transform = it’s own transform”?
Are you assigning to the parent the same transform that it has?
I’m a bit confused on how did this code make the Attacker be instantiated as a child of the spawner.

Thanks.

PS: Greatest course and instructor ever, amazing work.

Hi Waleed,

In the following statement;

newAttacker.transform.parent = transform;

…you are setting the parent of the newAttacker to be the transform of the GameObject the executing script is attached to.

The reason the attacker becomes a child of the spawner is that this executing code is a script component of the spawner, so the transform that you refer to above is the spawner’s transform (I’m assuming you code example is from the AttackerSpawner.cs script component attached to the spawner GameObject).

If we wrote that statement out in layman’s terms it would be, “set the new attackers parent to be the spawner”.

Hope this helps :slight_smile:

1 Like

Oh I see, I think I understand now.
I now understand that “Parent” and “Child” are part of the component “Transform”
I thought “Transform” was only about position, scale, and rotation.

Thank you very much Rob for the clarification and quick response.

2 Likes

You’re very welcome, for a little further clarity;

parent is a property of the Transform class. There isn’t a child property, although there are methods to get a child, or find a child within a Transform, these would typically return an object of type Transform.

Hope this helps :slight_smile:


See also;

1 Like

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

Privacy & Terms