Home > OS >  Unity ScrollRect not scrolling to a value
Unity ScrollRect not scrolling to a value

Time:11-22

I am using two arrow buttons (Up/Down). When pressed, they change the values for scroll rect in the vertical direction. Problem is, I am manually trying to change the value in my script but it is not changing at all. How do I fix this?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScrollViewSystem : MonoBehaviour
{
    private ScrollRect scrollRect;

    // Start is called before the first frame update
    void Start()
    {
        scrollRect = GetComponent<ScrollRect>();
        Scroll();
        
    }

    // Update is called once per frame
    void Update()
    {
        Debug.Log(scrollRect.verticalNormalizedPosition); //Outputs value as 1
    }

    public void Scroll(){
        StartCoroutine(ResetToMid());
    }

    IEnumerator ResetToMid() {
        yield return new WaitForEndOfFrame();
        scrollRect.verticalNormalizedPosition = 0.5f;
    }

}

CodePudding user response:

The code is fine, just make sure the Scroll View is on the right scroll direction (Vertical in your case).

enter image description here

Second thing is the Content GameObject's Rect should be covering the the content.

enter image description here

enter image description here

  • Related