- Home /
4.6 UI Canvas moves on Play, but not really?
I have a lot of canvases and child canvases and it's all going great.
Once in a while, and now again on a new group I'm working on, the canvas likes to reposition itself whenever I press play.
The inspector shows it's anchored position is still at 0,0, and if I change either of those numbers say to 1, it will jump back down to where it's supposed to be. It's almost like it's sticking the Bottom-Left corner into the center pixel by accident, but only on the first pass. I anchor almost all of my canvases in the Bottom-Left. I can also disable and re-enable the canvas and it will redraw itself properly.
I am using EventSystems and UI code quite a bit, but nothing is yet targetting this canvas, other than a parent script storing it as a GameObject for SetActive(true/false), which is going on with many other canvases just fine.
Seems like a bug, like I say loading the Bottom-Left anchor in the Middle by accident, but only for the first run, though it is not a perfect fit. If I hack some code in that changes any of the anchor data it will re-look at it and fix itself... but why?
That black corner in the middle of the screen should be another 300px left, and 200px down...?
I havent used the new UI system much but from what I gather, you only need one canvas for all your UI elements and you just Instantiate prefabs of the Images, Texts, Scrollbars, whatever when you need them, or disable/enable them from showing on screen when you need them? If I am wrong, let me know.
There's no "need" persay for it. I have lot's of windows within windows though... I've only been using the new UI stuff a week or two now, so I'm certainly no expert. I suppose I could be using empty transforms just as easily for a lot of stuff... but for example
$$anonymous$$ainUI(canvas)
-PlayerUI(canvas)
-ChatUI(canvas)
etc. In this case I have the $$anonymous$$ainUI which scales to fit the players screen and controls everything beneath it with that. A child of that, PlayerInfo is anchored in the top left with the player name/etc (can see in the pic) and has its raycasting disabled, so it will always be clicked through/non-interactable. reversely the ChatUI has its raycaster enabled so that the player can interact with it. Both however have their own separate anchors beneath the $$anonymous$$ainUI anchor off which their individual buttons/etc are anchored.
I don't know if it's necessarily proper, but it's working for me.
As a little update, I noticed while working on my laptop: the window drew up and to the right again, but I decided to ignore it and just try the new button I made. But, the new button wasn't where it was being drawn (offset like in the picture above). Somehow only the draw is being offset, the code/script all knows that that's not where it really is. Once I off/on'd the canvas to get it to redraw, I could use the buttons fine in their proper location.... weird, but again, work-around is not hard. Just surely not intended.
Just curious, the canvas that is jumping around, what is its Render $$anonymous$$ode set to?
I really only work with canvases set to use Screen Space- Overlay, which usually works. But it seems like when I first started, my canvas was having issues also. Creating a new canvas/project seemed to make things work for me, but it may not be the same issue. Did you try to create a new canvas and move all of its children over?
After SnStarr's comment I started using more RectTransforms ins$$anonymous$$d of new canvases to control the locations of child elements and I haven't been having the problem since.
Like you suggest though recreating it did seem to fix the issue sometimes back when I was running into it.
It was mostly strange because it would on mess up the first update. As soon as you moved the UI or something interacted with it it would realize it's own mistake and fix....
But in truth I haven't seen it since new years...
Your answer

Follow this Question
Related Questions
Different Normal vs Debug Mode values in Inspector. RectTransform bug may be? 0 Answers
Unity Android Screen Glitch 1 Answer
Problem moving a panel UI 0 Answers
Canvas lines showing at run time 2 Answers