- Home /
How to get localPosition to work on parented object C#
Sorry, this is probably really simple stuff, but I can't seem to find any similar post. I can't seem to get "localPosition" to work in conjunction with parenting. When I parent the instantiated object then it appears at the exact same position as its parents even though I am changing its "localPossition".
Simply put, the following code works perfectly. But if I un-comment the commented line of code, then all of the instantiated prefabs appear on top of each other at coordinate of the game object running the script.
public void showInputs(){
int inputCount = 0;
for(int a = 0 ; a < 3 ; a++ ){
for(int b = 0 ; b < 2 ; b++ ){
input[inputCount] = Instantiate(Resources.Load("InputPrefab")) as GameObject;
Vector3 temp = gameObject.transform.position;
temp.x += startingWidth/ Screen.width + (inputWidth / Screen.width) * (float)(a);
temp.y += startingHieght/ Screen.height - (inputHieght / Screen.height) * (float)(b);
temp.z = 5;
//input[inputCount].transform.parent = gameObject.transform;
input[inputCount].transform.localPosition = temp;
inputCount++;
}
}
}
Yep, you were right. the parents scale was (0,0,0). unfortunately, changing it to (1,1,1) now wrecks everything. I'll probably have to start from scratch with what you said in $$anonymous$$d. Thanks.
Answer by KellyThomas · Dec 28, 2013 at 07:15 AM
As they don't have a parent assigned then assigning a value to `.localPosition` is equivalent to assigning to .position. This would explain why things currently "work".
However if you assign a parent, then .localPosition is influenced by the parent's scale. Is the parent's scale something other than (1,1,1)?
Your answer