- Home /
UI GameObject created in code has rotation that can't be modified
I am creating a new GameObject and set it as a child of another UI gameobject:
child = new GameObject("Child", typeof(RectTransform));
child.transform.SetParent(transform);
The UI gameobject I set as the parent:
It does not have rotation. The canvas, which has a rotation (automatically matches the camera rotation):
and the child object resulting from the code snippet:
As you can see somehow the child object gets the negative rotation of the canvas. However I can not edit the rotation in code.
child.transform.rotation = Quaternion.Euler(Vector3.zero);
child.transform.rotation = Quaternion.identity;
i tried this with both the Transform.rotation as well as the RectTransform.rotation and it will always reset back to the default rotation on play.
Answer by AbandonedCrypt · Feb 18, 2021 at 05:04 PM
After crawling the doc some more I found out that Transform.SetParent
has an overload with a bool worldPositionStays
, which is true by default. Setting that to false eliminates the issue.
Your answer
Follow this Question
Related Questions
canvas screen space camera rotation issue 0 Answers
Rotating and Scaling "Stretched" UI Elements 1 Answer
How to get all gameObjects with a script on them 1 Answer
Weird Image Flickering Behavior on UI Canvas When Using RT Material 1 Answer
I Have a Button in Canvas, i Click anywhere except the button and it gets clicked 0 Answers