I have implemented a feature in my game where users can scroll horizontally between 5 pages. The third page is the middle one, so I want to set the starting page to the third page. However, the scrollbar’s value keeps resetting to 0.
Even if I set the value to 0.5 in the Inspector and start the game, it reverts back to 0 after stopping the game. Restarting the Unity Editor or reloading the scene also causes the value to reset to 0. I would appreciate any advice on why this is happening and how to fix it.
11
I think the ScrollRect
will be overruling the settings you made directly on the Scrollbar
component.
Rather try and set the ScrollRect.normalizedPosition
instead – e.g. like
void Start()
{
GetComponent<Scrollrect>().normalizedPosition = new Vector2(0.5f, 0f);
}
Alternatively afaik you can also directly overrule the position of the content – so if you want to center it on the ScrollRect you could also do e.g.
contentTransform.position = scrollRectTransform.position;