- Home /
transform.position vs interface position
I am working with Canvas and prefabricated Text. Through script i want to place the text one below the other. The problem is that obj.position += new Vector(0,33,0); where 33 is height of the object, is not actually 33 when looking at the inspector.
even translation of 1 through script is 38.5 in inspector. WHY?
here is the code
selfTransform = this.transform.gameObject.GetComponent<RectTransform>();
selfTransform.position = new Vector3 (0, 1, 0);
Answer by IgorAherne · Jun 15, 2017 at 12:01 PM
You need to look into RectTransform. Usually (definitelly in your case, unless your text is a 3D mesh, which it's not by your words) you need RectTransform. This component is by default swapped instead of traditional Transform, on all children of UI Canvas
To access it you would have
RectTransform _rectTransform = transform as RectTransform;
_rectTransform.pivot += Vector2.one*0.1f; //etc
m sorry, but i have been using RectTransform only. the code was not properly displayed, hence the confusion, but now i formatted it. As you can see, m using RectTransform only. Further more, the problem is not just here, even if i scale something to a value, it won't be scaled to that value, some other value is seen on runtime. I tried creating a new project, same issue. If i make x position into 30 using script, on runtime, i should see it at 30 right? m thinking of reinstalling unity, but then i'll have to set everything up again.
Your ui doesn't use the same coordinate system as objects in the world. You can use RectTransformUtility to make the conversions to and from canvas/world/screen space.
If you want ui objects to appear one under the other though, you should probably just be using a VerticalLayoutGroup component in the object that parents those items ins$$anonymous$$d of writing your own code for it.
Your answer
Follow this Question
Related Questions
I'm trying to displace a UI object in the opposite direction I move a scrollbar 1 Answer
Prefab Instance changing when loading scene in editor 0 Answers
Screen position of the uGUI object that deep in hierarchy 0 Answers
How can I accurately convert a Game Object's position to a sub Canvas/Rect screen position? 0 Answers
RectTransform.TransformPoint returning zero everytime after Unity 4.6.3 0 Answers