- Home /
Force ScrollBar to scroll down with ScrollRect
Hi. I'm keeping a expandable list of elements within my ScrollRect and I'm trying to automaticly scroll to the bottom of the rect each time a new element is added.
First I thought it should be done this way:
scrollRect.verticalScrollbar.value = 0;
But it did nothing, literally the scrollbar didn't change its value. So... the ScrollRect class sets the value of the scrollbar by itself. In this case I tried to set it with the ScrollRect instance:
scrollRect.verticalNormalizedPosition = 0;
But each time this line is executed, I get the error:
UnassignedReferenceException: The variable m_ViewRect of ScrollRect has not been assigned.
You probably need to assign the m_ViewRect variable of the ScrollRect script in the inspector.
I'm assured I have set up the "content" and "vertical scrollbar" properties of ScrollRect in inspector. Any ideas what am I doing wrong?
Just to be sure, some logs:
scrollRect.verticalScrollbar.value = 0.5f;
Debug.Log( scrollRect.verticalScrollbar.value ); => 0.5
Debug.Log( scrollRect.verticalNormalizedPosition ); => 0
// even after those lines value of the scrollbar in the inspector is still set to 1
Answer by frankcyli · Aug 05, 2015 at 09:47 PM
Works for me in Unity 5.1:
GameObject.Find ("Scroll Rect").GetComponent<ScrollRect>().verticalNormalizedPosition = 0.5f;
Answer by wowipop · Oct 14, 2014 at 09:51 AM
Canvas.ForceUpdateCanvases();
scrollRect.verticalScrollbar.value=0f;
Canvas.ForceUpdateCanvases();
well the first Canvas.ForceUpdateCanvases() is a must.. the second one just depend on what you do
Canvas.ForceUpdateCanvases();
scrollRect.verticalScrollbar.value=0f;
Canvas.ForceUpdateCanvases();
This code is perfectly works and it's very useful for automatically scrolling(vertical scrollbar).
Wow thank you, nothing else was working, but the
Canvas.ForceUpdateCanvases();
Works really well.
Answer by Rosecroix · Dec 02, 2014 at 09:52 PM
A much simpler solution is to do a
yourScrollRect.velocity=new Vector2(0f,1000f);
But I agree it would be more intuitive to change the scrollbar value, that was what I tried first too.
I don't think changing the scrollbar value would be a more intuitive solution as it is not required to set a scrollbar. So if you don't have one it will give a null pointer exception.
This does work - you can set the velocity (perhaps continuously in a coroutine, say) if you want to. You'd have to continually check it has not got to where you want it to go. It seems much easier to just trivially tween the .verticalNormalizedPosition
to whatever position you want.
Answer by cdpatel · Dec 28, 2016 at 08:39 AM
Try it. It is work for me.
Canvas.ForceUpdateCanvases ();
messageScrollRect.verticalNormalizedPosition =0f;
Canvas.ForceUpdateCanvases ();
This actually worked better for me than the other suggestions. Thanks!
Thank you veryyyyyy much... It really works better than the other suggestions.
Answer by dendriel · Jul 26, 2017 at 10:21 PM
Using Unity 5.6 my verticalNormalizedPosition can't be set to 0. That is, it is set to zero but looks like to update right after.
Thus, the verticalNormalizedPosition can't be set to round zero and for long messages some lines won't be visible.
To solve this "overwriting", it's necessary to wait for the canvas to update with a "yield return null".
Canvas.ForceUpdateCanvases();
// Wait.
yield return null;
scrollElem.verticalScrollbar.value = 0f;
Canvas.ForceUpdateCanvases();