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.