- Home /
Instance child global position
I am instantiating a prefab in different locations. This prefab contains some children objects. I can retrieve the position of the instances (as parents), but when I try to get the position of the children they always stay the same. How can I retrieve the correct global position of an instance of a prefab?
---------------------------------------------Fixed-------------------------------------------
I was using GameObject.Find().transform.position to get the positions of the children. But GameObject.Find() does a global search and because the children were belonging to instances of the same prefab they had the same name and my GameObject.Find() would return me always the same object(the child in the first instance).
However, I found out that GameObject.transform.Find() does a local search instead and given the correct path it returns the transform for the right child.
I ended up getting the positions of the children like this:
var prefab:GameObject; //This is my prefab, a section of the floor with some obstacles
function Start () {
//The first instance of the section placed at the origin
var instance1:GameObject = Instantiate(prefab, Vector3(0.0f, 0.0f, 0.0f), transform.rotation);
//The second instance of the section placed at an offset
var instance2:GameObject = Instantiate(prefab, Vector3(0.0f, 0.0f, 20.0f), transform.rotation);
//Getting the position of the child "O1" of the first instance
var pos1:Vector3 = instance1.transform.Find("obstacles").Find("O1").transform.position;
//Getting the position of the child "O1" of the second instance
var pos2:Vector3 = instance2.transform.Find("obstacles").Find("O1").transform.position;
//Printing the positions of the children
Debug.Log(pos1);
Debug.Log(pos2);
//Printing the positions of the parents
Debug.Log(instance1.transform.position);
Debug.Log(instance2.transform.position);
}
And the section prefab is structured like this:
Answer by Caiuse · Dec 02, 2012 at 10:56 PM
`transform.position` returns the global position of any object regardless of its parenting. If the prefabs children's local positions are (0,0,0), then printing the position `print( child.transform.position );` would print the same as its parents transform.position.
On the other hand if you want the local position for the child object relative to it's parent, use this `transform.localPosition`
That's what I thought but for some reason it seems it's not this way.. one second.. I will post some screenshots
Just added some screenshots. This is what I see in the inspector and I get the same in code. You can obviously see that the positions of the gizmos are different but the values are the same.. how is that possible?
The inspector only shows the local position, there may be a way to change that but I'm not aware of it. Print the objects position by making a little script function Start(){ print(YourTransform.position); }
. I'm assu$$anonymous$$g that you only need the objects global position for program$$anonymous$$g purposes... in that case just using transform.position will work for you.
OR this answer gives you a very handy little editor extension script
Thank you very much for the comment. Everything you say makes sense. But the whole problem started from getting the same values in code. I use transform.position as Caius suggests and I get the same values for the positions of two objects which are clearly in different positions in space.
Your answer
Follow this Question
Related Questions
I need help with the location of instatiated objects on the scene and correct interaction with this 2 Answers
How to follow multiple clones positions of an instantiate prefab having a velocity ? 1 Answer
How can I get a GameObject's transform in game and set that to a variable? 1 Answer
Use GameObject's global position instead of local position 3 Answers
How do I get the opposite position of an object that spins around another object... 0 Answers