Search Unity

Scrolling the scrollbar to bottom when new text line is added

Discussion in 'UGUI & TextMesh Pro' started by David2256, Aug 17, 2017.

  1. David2256

    David2256

    Joined:
    Aug 17, 2017
    Posts:
    2
    I made a text area, which is scrollable with scrollbars, and from scripts I can add new lines to it. I want to make the scrollbar to scroll down to the bottom when a new line is added. As I was searching for a solution on the internet I found this:

    http://answers.unity3d.com/questions/801380/force-scrollbar-to-scroll-down-with-scrollrect.html


    They wrote that the following code works (but no other explanation):

    GameObject.Find ("Scroll Rect").GetComponent<ScrollRect>().verticalNormalizedPosition = 0.5f;

    It seems to me that they are in a more advanced level because this is all the help they needed, but I cannot figure out how to actually implement this solution. From other sources, and from looking in the documentation, it seems like that the verticalNormalizedPosition is what I need to use, but I don’t know how.

    I tried using the following lines of codes, but It is still not working:

    public ScrollRect scrollRect;

    scrollRect = GetComponent<ScrollRect>();


    Can somebody explain what is the problem? Or at least point me in the right direction. The Unity tutorials and other sources online unfortunately doesn’t cover this exact situation.
     
  2. David2256

    David2256

    Joined:
    Aug 17, 2017
    Posts:
    2
    Next morning after posting this I was messing around with this problem and I was able to solve it. Looks like I just had to sleep on it.

    My solution was to create this for the inspector:

    public ScrollRect scrollRect;

    Than do this at start:

    scrollRect.GetComponent<ScrollRect> ();

    Than I was able to use the following code after writing a new line into the textbox:

    scrollRect.velocity = new Vector2 (0f, 1000f);

    This was suggested by the user "Rosecroix" in the linked unity answer. Then I dragged the appropriate piece from the hierarchy window into the inspector.

    Before using this I tried another way that was working but that never put the scrollbar all the way to the bottom. There was always some lines still hidden. This one does.