- Home /
UI 4.6 Scrollbar.value sets to 1 first time but not when opened again
Creating a menu from prefabs using a scrollrect and scrollbar as shown in Stuart Spences YouTube Example.
The menu works fine an I worked out that setting the Scrollbar.value = 1 means it always starts at the top of the menu.
So I reference the scrollbar and set it's value. It works perfectly when the Menu opens first time but not when I close the menu and open it again.
Debug.Log shows it is setting the value to one but it must then set it to .5
No idea why, anyone seen that or know a way to force it to stay at 1 when the menu opens?
allScrollbar = GameObject.FindGameObjectWithTag("allScrollBar");
allScrollbar.GetComponent<Scrollbar>().value = 1;
Debug.Log ("Setting the scrollbar to 1 at " + Time.time + " vlaue = " + allScrollbar.GetComponent<Scrollbar>().value);
Works First Time:
Then Fails When Menu Closed And Re-Opened
Answer by d_kieffer · Dec 30, 2014 at 08:00 PM
I had a similar problem that seemed to stem from using the Content Size Fitter component on the content that I wanted to scroll. I could set the Scroll Bar's Value to 1 but it would always reset itself to 0.5.
THE FIX: In the Rect Transform component of your scrollable content the Pivot Y value is probably set to 0.5. Change this Pivot Y value to 1. That should force your scrollable content to always start from the top.
Thanks @d_kieffer and sorry for the slow response.
Yep I worked this out as well but stupidly failed to update this post so accepting your answer as absolutely the correct way to handle this.
Pivot's, it turns out, are really important when creating instantiated menu items with the new UI.
Same exact issue, changing the pivot worked for me too. Thanks d_kieffer!
I had the exact same issue, was trying to solve it for a while now. Thanks D_kieffer !
For newcomers to this thread, I can confirm that this solution does work in every version of the engine. I'm currently using 2017.3.1f1
Answer by Mmmpies · Dec 18, 2014 at 08:53 AM
OK I set a check to record the menuOpenTime then in Update checked Time.time to see if it was > menuOpenTime + 0.1f.
Still failed so set that to 0.2f and it fixed it.
No idea why it always works first time then menu opens but on subsequent times it fails unless I check it. The menu is doing exactly the same thing each time I open it but 100% of the time it works when the menu first opens but fails on all occasions I open it after that.
This is just a test scene but in reality I'd want to stop time when in menus so I suppose I'd need to count passed time with fixedDeltaTime.
Maybe something that needs fixing in the UI but then it's still early days for the new UI.
Your answer
Follow this Question
Related Questions
4.6 UI - how to make a dynamic scrollbar? 2 Answers
TextMeshPro dropdown default value change 0 Answers
Move gameobject to button in new 4.6? 0 Answers
Unity 4.6 Beta Controller Menu Button Movement 0 Answers
Unity 4.6 - Rotating with Mouse Issue 0 Answers