Problem with ScrollView

I am getting the error "Operation is not valid due to the current state of the object System.Collections.Stack.Peek ()" while building and animating a GUI accordion for smooth moviment using scrollview. What could be happening?

Thanks in advance.

Here is the Code:

void OnGUI()
{
    GUISkin skin = GUI.skin;
    GUI.skin = sknScrollbar;
    if (this.Display)
    {
        GUILayout.BeginArea(this.rtComponentPosition);
        vtScroll = GUILayout.BeginScrollView(vtScroll, GUILayout.Height(Screen.height - rtComponentPosition.yMin), GUILayout.Width(this.rtComponentPosition.width));
        ListChilds(root.childs);
        GUILayout.EndScrollView();
        GUILayout.EndArea();
    }
    GUI.skin = skin;
}

void ListChilds(IList<Item> childs)
{
    foreach (Item item in childs)
    {
        item.scrollHeight = getScrollSize(item);
    if (item.Nome == "PINTAR" || item.Nome == "DECORAR")
        {
            accordionSize = 29;
        }
        else
            accordionSize = 25;

        if (item.Nome == currentItem)
        {

            GUILayout.BeginScrollView(new Vector2(), GUILayout.Height(accordionHeight));
            if (item.isOpened && !item.closening && accordionHeight < accordionSize * (1 + item.childs.Count))
            {
                accordionHeight += 2;
            }

            if (item.closening == true && accordionHeight > 29)
            {
                accordionHeight -= 2;
            }
            if (item.closening == true && accordionHeight <= 29)
            {
                item.isOpened = false;
                item.closening = false;
            }

        }

        if (GUILayout.Button(item.Nome, item.style))
        {

            if (item.isOpened == false)
            {
                item.isOpened = true;
            }
            else
            {

                item.closening = true;
            }

            item.SetClick();
        }

        if ((item.childs != null) && (item.isOpened))
            ListChilds(item.childs);

        if (item.Nome == currentItem)
        {
            GUILayout.EndScrollView();
        }
}

Julian, your hint helped alot! Thank you.

I’ve encountered same nasty bug with “GUILayout.BeginHorizontal”. Spend few hours in debugging before figured out the source of the problem. After finding your answer here, I ended up with code like this:

GUILayout.BeginHorizontal(); 
bool isHorizontalBlockActive = true; 
	
if(GUILayout.Button("Start complex process"){	
	MyComplexProcess();	
	isHorizontalBlockActive = false; 			
}

// ...more buttons here
 
if(isHorizontalBlockActive)
	GUILayout.EndHorizontal();

Looks ugly, but works fine :slight_smile:

Well, I found out what was happening. the "item.SetClick();" changes the currentItem string, and doing that, the condition is true for the EndScrollView() before the BegingScrollView exists, as it tries to End something that doesn't yet exists, the error is returned.

I fixed the problem adding a bool to the End condition that is false on SetClick() and only is only set true after BeginScrollView().

Hopes it helps anyone someday...