Setting a parent of instantiated object negate all axis
Hi, I have problems with generating prefabs in parent layer. I have two layer - Level0 (0,0,0) and Level1 (0,0,-1). When I create new prefab under Level1, it has swapped axis (0,0,1). All axis are swapped - when Level1 is (2,3,4), new object will be at (-2,-3,-4). What I am doing wrong?
Script:
Level1 = GameObject.Find("Level1");
Debug.Log(Level1.transform.position.ToString()); //0,0,-1 -> correct
GameObject newPipe = Instantiate(pipe, new Vector3(0, 0, 0),Quaternion.identity) as GameObject;
Debug.Log(pipe.transform.position.ToString()); //0,0,0 -> correct
newPipe.transform.SetParent(Level1 .transform);
Debug.Log(newPipe.transform.position.ToString()); //0,0,0 -> correct
But in the game the position is (0,0,1).
Answer by hexagonius · Mar 16, 2016 at 08:41 PM
The default value for the second parameter of SetParent is true.That means the childed object keeps it's position. since that's 0,0,0 at start it's the negated value of the new parent. set the parameter to false. Read about it here:
http://docs.unity3d.com/ScriptReference/Transform.SetParent.html
Your answer
![](https://koobas.hobune.stream/wayback/20220612072110im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiate not working correctly (Please help!) 1 Answer
Can't Instantiate an object as child of another 1 Answer
Cannot convert UnitEngine.Vector3 to UnityEngine.Transform when using .postition 1 Answer
Place instantiated object directly behind another 0 Answers
How to check for the previous position of the player? 0 Answers