RectTransform.anchoredPosition changes, but element is not visually updated
I've been trying to make a menu that whenever I click in an actor, the menu's position change so it's next to the selected actor. The first time (and only the first) I execute the following code, everything works fine:
element.anchoredPosition = screenPosition;
The element is correctly position. However, when I click in another position, the element's position in the editor CHANGES, but visually it's the same. Even the buttons inside are clickable in the new position, but Unity keeps showing the element in the older position.
I tried Canvas.ForceUpdateCanvases(), SetActive() and some other stuff, but nothing seems to work. Here's an image when seeing from the editor (you can the the selection border shows the new position but the UI is showing on the old one): https://i.imgur.com/Yl8LTXB.png
Thanks in advance.
Hey,
You're not alone, I am having the exact same issue.
I have an array of elements and the first time I set "anchoredposition" and "sizedelta", everything works fine and all elements are positioned and sized correctly.
But the second time when I set the position and size, nothing gets visually updated although when walking through the hierarchy the editor "ui outline" appears in the correct position with the correct size.
As soon I change anything on the object, then suddenly the visual snaps into place...
If you figured something out, please post it here.
Thanks!
Unity support has answered me after I filled a bug report. Apparently it's a known issue that's being tackled. They suggested me using Unity Beta.
Another guy who commented in my reddit post said that Unity support suggested deleting Project Settings and Library, but I'm not sure how this'd play out. In any case, here's the thread: https://www.reddit.com/r/Unity3D/comments/72obz8/recttransformanchoredposition_changes_but_element/
Hey,
Thanks for the reply, good to know that Unity is aware of it. I currently have gotten around it by doing an ugly hack:
In an Update() in a script, I change the value of the parent (the gameobject that contains all my child objects that refuse to visually update) RectTransform.size$$anonymous$$in by 1 pixel, and reset the value back to what it's supossed to be the next frame.
This seems to force the canvas to recalculate and visually update everything inside.
It's a very ugly way of doing it, but it's the only way so far I've found without changing Unity version.
Answer by unity_uH_1eZq6Ny1FIA · Sep 28, 2017 at 09:22 PM
Several people have filled a bug report about this issue. There's some workarounds at the moment:
Using position instead of anchoredPosition with a call for GameObject.SetActive(true) in the line before it (Va11ar);
Changing RectTransform.sizeMin by 1 pixel at an Update() call and resetting it next frame (Simon_SG)
Deleting Project Settings and Library (suggested by Unity Support for Simon_SG, but not tested)
If anyone wants to track the issue and upvote it, just check it out here.
Your answer
Follow this Question
Related Questions
How to offset a RectTransform? 0 Answers
UI Canvas Anchor points are stuck in the bottom left corner and disabled when in overlay mode. 1 Answer
Resize UI Panel to the same size as its parent Canvas through code 0 Answers
How to tell if a RectTransform is within the visible area of a ScrollRect 1 Answer
Updated with UI text element not correct (same frame) 0 Answers