ScrollRect resetting scroll position on content change

Is there a way to tell ScrollRect not to reset scroll position when content changes? The scrollable content size changes during runtime as items are added and removed - problem is that the scroll position is reset whenever the content rect transform is changed. I want the scroll position to remain. Is there a way to do this?

I found the problem. Having any kind of layout group of the view port will change the behaviour when objects are added to the content pragmatically. Also making sure the Contents anchor points and pivot are set correctly. For a Vertical scrollRect you want them to be like this 98313-recttransform-anchors.png

Create a ScrollRectExtensions class

public static class ScrollRectExtensions
{
    public static float GetValue(this ScrollRect scrollRect, float value)
    {
        return scrollRect.horizontal ?
                scrollRect.horizontalNormalizedPosition :
                scrollRect.verticalNormalizedPosition;
    }

    public static void SetValue(this ScrollRect scrollRect, float value)
    {
        if (scrollRect.horizontal)
        {
            scrollRect.horizontalNormalizedPosition = value;
        }
        else
        {
            scrollRect.verticalNormalizedPosition = value;
        }
    }
}

In your code, call:

void Awake()
{
            StartCoroutine(FixScrollRects());
}

IEnumerator FixScrollRects()
{
            yield return new WaitForEndOfFrame();
            yield return new WaitForEndOfFrame();
            foreach (var scrollRect in GetComponentsInChildren<ScrollRect>())
            {
                scrollRect.SetValue(0);
            }
}

Double WaitForEndOfFrame(); was necessary in my case. (2019.3.1f)

did you solve the problem?

Thanks for this solution @Brogan89. Still took me awhile to get it working.

For posterity, I attached pictures of my setup:

alt text

alt text

alt text

This worked for me, attached this to the scrollbar

void OnEnable()
{
    StartCoroutine(resetScrollPos());
}

IEnumerator resetScrollPos()
{
    yield return null; // Waiting just one frame is probably good enough, yield return null does that
    gameObject.GetComponent<Scrollbar>().value = 1;
}

I have the same issue, the images in my content reset when I pressed the play button, I use a VerticalLayyoutGroup, can anymore give me the solution, please?
THX