- Home /
How do I get the global position
I have a prefab called Section1 that is organized like this:
I am instantiating this prefab twice in 2 different locations.
Now I am printing the positions of the Child "O1" for both instances and I get the same location.
Why is that? How do I get their absolute global position?
This is my code:
var prefab:GameObject;
function Start () {
var instance1:GameObject = Instantiate(prefab, Vector3(0.0f, 0.0f, 0.0f), transform.rotation);
var instance2:GameObject = Instantiate(prefab, Vector3(0.0f, 0.0f, 20.0f), transform.rotation);
var pos1:Vector3 = instance1.Find("O1").transform.position;
var pos2:Vector3 = instance2.Find("O1").transform.position;
Debug.Log(pos1);
Debug.Log(pos2);
Debug.Log(instance1.transform.position);
Debug.Log(instance2.transform.position);
}
And this is the output:
------------------------------------------FIXED-------------------------------
Both Dave and valorik were right. valorik said that GameObject.Find() does a global search and everytime I was doing it I was getting the same object; Dave suggested I should use `instance.transform.Find("O1").transform.position` , but that would give me a nullrefference error. I managed to fix that error by using this code:
instance.transform.Find("obstacles").Find("O1").position;
I'm not really sure who to give the credit to. If Dave updates his answer to the code I mention above I will accept his. Thank you very much for your help.
Yes the last two lines are right. The first two ones are the problem. They should be different vectors as the children are at different positions in space. The first two lines in the console are the positions of the child "O1" in the two instances. $$anonymous$$y reasoning says that if the parents are offset(visible in the last two lines in the debugger) then the children should be too(the first two lines);
I need the absolute global positions of the child "O1" for both instances and those positions should be different
Answer by valerik · Dec 06, 2012 at 01:48 AM
var pos1:Vector3 = instance1.Find("O1").transform.position;
var pos2:Vector3 = instance2.Find("O1").transform.position;
I'm not sure about this.... because if i remember correctly, find works globaly, so you are getting two times the same object... try name them in a different way.
EDIT: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html
That make sense. But what I want to do is import my prefab and then instantiate is continuously and have my player follow some generated points which are children of the instances(let's say I want my player to follow "O1" from instance to instance). Is there really no way of doing a local search?
Or is there maybe a way I could retrieve one of its children by name?
you can try to use: (GameObject)instance1.GetComponentInChildren("01")
or use: GameObject [] o = instance1.GetComponentsInChildren(); then you iterate on the array and search it by name. or tag. i think it works, also maybe there are more efficent solution ;)
Answer by DaveA · Dec 06, 2012 at 01:43 AM
I think you want to Find off the transform, not the Gameobject:
Try this:
var pos1:Vector3 = instance1.transform.Find("O1").transform.position;
Your answer
Follow this Question
Related Questions
Associate objects to a prefab 1 Answer
How to get the instance of a child in a prefab object? 1 Answer
I need help with the location of instatiated objects on the scene and correct interaction with this 2 Answers
Instantiate prefab as child - Won't work, don't know why. 1 Answer
Instantiated object is destroyed in scene, but still logs as existing in console. 1 Answer