Little question about accessing children position
Hi ! My question is a little bit ridiculous maybe, but i try to understand everything and i don't want to let a blank space somewhere.
in this example of the transform :
public class Example : MonoBehaviour
{
// Moves all transform children 10 units upwards!
void Start()
{
foreach (Transform child in transform)
{
child.position += Vector3.up * 10.0f;
}
}
}
i really don't understand the logic behind "Transform child in transform"
What unity really does here ? why accessing the gameobject by the transform class and not by the gameobject itself ? is it necessary to access the position after ? and why are we using "in transform "?
Maybe that it's just some C++ basic stuff and so i should explore more in this way ? Thanks a lot !
Answer by Hellium · Sep 14, 2020 at 09:32 AM
A gameObject is nothing but a container for components. It's the Transform component which actually contains all the information of position, rotation, scale, and hierarchy in the "world".
The Transform
class implements the IEnumerable
interface. The implementation of the interface's method GetEnumerator()
returns an enumerator allowing you to call the foreach
loop and accessing all the children of the transform one after the other.
Oh i didn't know that Transform component contain hierarchy information ! That's also why we are using the transform to set the parent of an object for exemple. ! Thanks a lot !
Answer by streeetwalker · Sep 14, 2020 at 09:37 AM
@Nujazzbes, good questions! Here is my intuitive explanation:
A GameObject is the fundamental "in game" element in Unity, and a GameObject is nothing without a Transform component - it gives the GameObject a representation in space, even if there is no "physical" component attached (e.g. a mesh or a sprite).
The Transform is what makes the GameObject "real" in the game, even if it has no other components, in which case it is an "empty GameObject" or just a point in space.
A GameObject can have children, which simply means that other GameObjects can be tied to the parent GameObject such that any transformation applied to the parent is applied to the children. (Even an empty game object can have children).A GameObject can only do so because it is made "real" by its Transform component.
The code of Unity is designed such that Transform component contains references to the Transform components of its child GameObjects. So a the code definition of the Transform component also contains a collection variable - a list (it may or may not be a C# list - I haven't looked at the code) of references to all the Transform components of its children.
In the loop statement, 'child' is simply a variable name - it could have been "nujazzbes" or "x" or anything. But 'child' is apropos because we talk about 'child' GameObjects. So the loop gives us access the collection of Child Transforms that is defined in any given Transform, if the GameObject has children.
Does that help?
Oh i didn't understand that child in foreach were a variable.... So if i understand well, unity know that when we are using this function :
foreach (Transform hf in transform)
Automatically we are talking about child right ?? Thanks a lot for your answer
Yes, that is exactly right. You could write it as:
foreach (Transform lifeIsGreat in transform){
lifeIsGreat.position = ...
}
foreach is a convenient method of iterating over an enumerable datatype, but, according to the official Unity line, an expensive on mobile devices should be avoided where ever you can there.
The actual collection of child transforms is not exposed in the Transform class. Likely because the Unity engineers don't want us mucking about with that collection directly.
You can access it indirectly using foreach, or this indirect method, for example this does the same thing as foreach sample above:
for( int i = 0; i < transform.childCount; i++ ){
transform.GetChild( i ).position = ...
}
Ok i think i got it right ! thanks a lot for your help !!