- Home /
Rect transform position keeps changing value randomly
I've got a row of buttons I'm trying to position manually. I set their rect transform X position to 300 like so:
After I input it, the position changes to something close, but each rect transform is incorrectly positioned by some random floating point value. Here you can see that it's changed itself to 299.2984. Sometimes it'll be off by a value less than 0, sometimes it'll be four or five whole units off, all of its own accord, and it will change every time I try to put a new value into this field.
I've tried shutting off all the other scripts I've got attached and changing the anchor point to different things, but nothing seems to stop this from happening. Does anybody have any insight on what this is or how to fix it?
This is probably due to floating point inaccuracies. Rect transforms are pretty tricky, and some stuff they do it automaticly, based on your settings. $$anonymous$$ake sure to check out https://docs.unity3d.com/$$anonymous$$anual/class-RectTransform.html AND especially https://docs.unity3d.com/$$anonymous$$anual/UIBasicLayout.html
I suspect if you specify the rect transforms differently, like changing anchor settings, or using raw mode, this issue will go away.
"sometimes it'll be four or five whole units off" <- I've never seen that. If you really have that you should report a bug
The tiny differences, yes, it's pretty common and shouldn't be a problem, they're always too small to make a difference. Also, it's not something you can or should fix, it's something that shouldn't be a problem at all for the game. If 0.0016 difference in x is visible in your game you're probably doing some weird stuff with scales and the canvas scaler, otherwise a 300 and 299.9984 should translate to the exact same pixel in the screen. Also, if you're planning to use that value in code you shouldn't expect exact values working with floats, so again, it shouldn't be a problem if you know how to work with them.
I do get that putting 0 and seeing it turn into something like 2.35e-10 can make it look like it's not working O$$anonymous$$ and can make people obsessed with getting the exact value, but like @Glurth said it's related to how floats and the whole engine works, so you have to accept that it will happen every now and then.
Nope. Glurth was right. Floating point error. The menu it was nested inside of was scaled to an extremely tiny size, less than 0.01. I'm not sure how or why it got scaled to that size, but that caused floating point error in all of its child GameObjects, which looked rather alar$$anonymous$$gly like involuntary random jumps of four or five entire single units in local space.
Answer by NickCaligo42 · Aug 28, 2016 at 12:05 AM
Figured this out largely thanks to @Glurth's comment. Although it seemed like several entire units was a bit much to be attributed to floating point error, it isn't if your object is nested inside something that's scaled. This was a result of the entire menu being scaled to an extremely tiny size (less than 0.01) in order to fit the camera. After fixing the scale, the rect transform's position values behaved correctly. I'm still not sure what led to the menu getting scaled to begin with, but imagine it had something to do with my noodling with different canvas settings.
Answer by lucasnuke · May 11, 2018 at 09:52 PM
For anyone for whom @NickCaligo42's solution doesn't work: I encountered a similar effect when dealing with a ScrollView->Viewport->Content hierarchy, where the Content position value kept changing when selected or the game run.
The issue had to due with the Viewport anchors being set to stretch and fill the size of the ScrollView, and the Content height specified in game units. I was able to fix it by changing the Viewport anchors so that only the non-scrolling dimension is set to scale to the size of the Scroll View, with the scrolling dimension anchors set to an edge and the width/height specified in game units (probably the same size as your Scroll View).
(Additionally, it seems that the the content must be larger than the viewport).
Example: ScrollView->Viewport->Content
Scroll View has a width and height of 100x200, and you want it to scroll vertically. You have the Viewport anchors set to X-min/max: (0,1), Y min/max: (0,1), and Left/Right/Top/Bottom set to 0. You have Content anchors set to X-min/max: (0,1), Y min/max: (1,1), with a height of 300. Pos Y keeps changing randomly.
To fix, change the Viewport anchors to X-min/max: (0,1), Y min/max: (1,1), and height to your Scroll View height (should be changed automatically).
Hope this helps!
Answer by gabiedc · Aug 02, 2020 at 02:39 PM
I had a very similar issue in Unity 2019.4.2f1, so its still not totally fixed, but for me the solution was making sure that the Content's height was equal or larger than the Scroll View's height. Something about the Content's height being larger causes it to bug out slightly.
Your answer
Follow this Question
Related Questions
Stacked UI Panels do not position as expected 0 Answers
Button rect transform top 0 Answers
Moving RectTransform over another RectTransform under GridLayout 0 Answers
Move a UI element with respect to canvas scaler 0 Answers
Snap UI Text inside canvas 1 Answer