Reputation: 82
I'm currently developing a scene with a ScrollView in Unity. The component of the GameObject is called ScrollRect. I add game objects to the scroll view via code and set the scroll position to 0 in the Start() method, which means that the scroll rect should be scrolled all the way down.
I know from log messages that the objects are being added before the Start method is called in my script. I know from a change listener that after I set the scroll position to 0, it will be set back to 1 afterwards. What could be the reason? It's definitely not happening in my code.
public void Start()
{
GetComponent<ScrollRect>().verticalNormalizedPosition = 0;
}
A lot of layout-technical things happen in the ScrollView. I have several Layout Groups and Layout Elements and Content Size Fitter. Can it be that the sizes of the objects are only set after the start method is called and then everything shrinks and the scroll position remains at 1 when the object become big again? How could I solve the problem?
Upvotes: 0
Views: 510
Reputation: 82
I found a solution:
public void Start()
{
Invoke("SetScrollPosition", 0.001f);
}
public void SetScrollPosition()
{
GetComponent<ScrollRect>().verticalNormalizedPosition = 0;
}
Does someone know a better solution or does someone understand what the problem is? Is my suggestion with the layout-components right?
Upvotes: 0