Search Unity

How to set the scrolling position of a scroll bar?

Discussion in 'UGUI & TextMesh Pro' started by uiniti, Jan 21, 2016.

  1. uiniti

    uiniti

    Joined:
    Feb 4, 2015
    Posts:
    74
    I'm on Unity 5.2.3p3 and I want to reset the scrollrect/scrollbar position each time the user enables them.

    Setting
    UI.Scrollbar.value has no effect
     
  2. DWilliams

    DWilliams

    Joined:
    Jan 12, 2015
    Posts:
    63
    I haven't used many scrollbars but I think they're linked to the scroll rect content position and size compared to the viewport size. So if you move the scroll rect content to the position you want, the scrollbar will adjust accordingly.
     
  3. uiniti

    uiniti

    Joined:
    Feb 4, 2015
    Posts:
    74
    my problem is that the scroll rect content is changed at the same time
     
  4. devsh42

    devsh42

    Joined:
    Mar 16, 2017
    Posts:
    2
    I am using this hope it helps.

    using UnityEngine.UI;

    // my unity Scrollbars name inside unity is Scrollbar
    // variable for assigning GameObjects to
    private GameObject thisGameObject;
    // create variable to hold Script
    private Scrollbar ScrollbarScript;

    void Start()

    {
    // call function to find game object and script attached to it ( object name in unity, script name attached to object)
    getGameObject("Scrollbar", "Scrollbar");
    // call to set Scrollbar to top -- will change depending on scroll option (bottom to top or top to bottom) -- range 0.0 to 1.0
    ScrollbarScript.value = 1f;
    }

    private Object getGameObject(string object, string objectClass)

    {
    // find GameObject
    thisGameObject = GameObject.Find(object);

    // conditional for items with different scripts -- could use switch statement
    if (objectClass == "Scrollbar")

    {
    // find component Scrollbar (Script) of GameObject Scrollbar (unity GameObject)
    ScrollbarScript = thisGameObject.GetComponent<Scrollbar>();

    }
    // returns GameObject -- this is not needed for this example, but might be useful
    return thisObject;

    }
     
    Last edited: Mar 27, 2017
  5. XiongGuiYang

    XiongGuiYang

    Joined:
    Sep 5, 2016
    Posts:
    14
    using System.Collections;
    using UnityEngine;
    using UnityEngine.UI;
    public class SRect : MonoBehaviour {

    public ScrollRect _sRect;//
    // Use this for initialization
    void Start () {
    StartCoroutine(SetValue());
    }

    IEnumerator SetValue()
    {
    yield return new WaitForEndOfFrame();
    if (_sRect != null)
    {
    _sRect.verticalNormalizedPosition = 0.5f;//Range(0.0f,1.0f)
    }
    else
    {
    Debug.LogError("ScrollRect _sRect==NULL");
    }

    yield return null;
    }
    }
     
  6. Smithy43

    Smithy43

    Joined:
    Sep 8, 2012
    Posts:
    11
    Setting UI.Scrollbar.value directly works for me (Unity 2018.4) however I have found it necessary under some scenarios to do this after waiting for the end of frame.

    The wait is possibly required if the scrollbar is being enabled for the first time in the same frame the value is being set?