- Home /
Positions of Objects in Unity - World Position, Transform Position, Inspector Position, Local Position, Prefab Positions, Center of Objects for Parents & Childs
Hello everyone,
i have been practicing Unity now since almost a month and i really thought i did make some progress. Apparently I haven't (pure frustration) since I seem to lack the very basic understanding of the positioning of objects in the game world and no matter how many posts and documentation i read - I don't get it.
Lets start simple: i create a white cube in the world. As far as i figured out, what i see in the transform in the inspector IS the actual position of the cube in the gameworld. The little box with the x,y,z arrows is always displayed at the center of the object relative to its position in the world. Thats also means I see that center correctly in #Scene view. So far so good. Now the confusing parts starts:
I turn the white cube into a prefab. How do the transform positions in the inspector and the transform positions inside the prefab correlate? Especially if i add children in the prefab? Will everything stay the same if i click on the prefab object in the hierarchy panel?
Now i add a script to the cube (outside of the prefab but on the prefab (outside not inside) and store its position with test=this.gameobject.transform.position. What position will i get on Debug.Log (test)? Will this change if i add children inside or outside the prefab?
Now i create 3 different cubes. I put those 3 different cubes into an empty gameobject. If i now get the position of the empty gameobject, what will I get? The actual position of the empty gameobject or all the objects combined and then their center?
And last but not least - the little box that has the x,y,z arrows on it - when will it be in the actual center of that gameobject i clicked on and thus tell me the actual position of that object in the world in the case it has children and is not a prefab? Or is it irrelevant wether something is a prefab or not?
Any answers would be much appreciated and free me from my positioning nightmare.
P.s. i tried both transform.position and transform.localPositon
Answer by macsimilian · Sep 25, 2021 at 10:08 PM
I think you are making this more complicated than it is.
Any instance of a prefab has its own field values. You can reset them to default and they will take on the values of the prefab. So you can place instances of a prefab and move them around, and they will have their own, unique transform. Instantiating a prefab from code typically involves assigning the new instance's transform. It doesn't matter whether an object is an instance of a prefab or not. The transform shown in the inspector is correct.
Adding children to a parent doesn't affect the parent's transform.
If you Debug.Log(this.gameobject.transform.position) on Start, you will get the same value displayed in the inspector (unless you move something before then).
The transforms of parent and child elements are separate. However, the transform of the parent essentially gets added to the transform of the child, in order to determine the child's absolute position. In this way, the child "follows" the parent. You can determine this overall position of a child added to its parent with Position, and the position relative to the parent with localPosition.
I put those 3 different cubes into an empty gameobject. If i now get the position of the empty gameobject, what will I get? The actual position of the empty gameobject or all the objects combined and then their center?
You will get the actual position of the empty game object (no averaging of values or calculated center!). Changing the position of the empty object will also move the child cubes.