GUILayout.Box BeginScrollView Issues

Hi everyone, my scrollview script is acting weird. When I try to put a GUILayout.Box into void OnGUI it appears as an extremely tiny box and no matter what I can’t change its size. I can’t quite figure out what’s causing this. I think it might have something to do with the BeginScrollview function in my script but I’m not positive.

private Vector2 scrollPosition;
    public int Switchint = 1;

      void OnGUI()
            {

           GUI.BeginGroup(new Rect(525, 275, Screen.width/2, 
            Screen.height/2));
           GUILayout.Box("20", GUILayout.Width(200));
             switch(Switchint)       
          {         
             case 1:   

              scrollPosition = GUILayout.BeginScrollView(scrollPosition,
                    GUILayout.Width(150), GUILayout.Height(150));
             GUI.EndScrollView();
                GUI.EndGroup(); 
                break;                  
             case 2:            

             break;             
             case 3:            

           break;
             default:            

                break;      
           }

You need to use GUILayout’s BeginArea function instead of GUI.BeginGroup.

BeginGroup changes screen coordinates for subsequent GUI.{x} calls; BeginArea actually forces GUILayout to draw into a specific area.

I switched to BeginArea and that fixed my Box problem but now for some reason I can’t move my scrollview with the coordinates from BeginArea. It’s just stuck on the edge of the screen.

edit: I tried removing the beginscrollview to see if that would fix the problem. it did but I need the beginscrollview for my script. Why does Beginscrollview seem to cause a lot of problems?

Here is what the code looks like now

    private Vector2 scrollPosition;
        public int Switchint = 1;
     
          void OnGUI()
                {
     
               GUILayout.BeginArea(new Rect(525, 275, Screen.width/2,
                Screen.height/2));
               GUILayout.Box("20", GUILayout.Width(200));
                 switch(Switchint)      
              {        
                 case 1:  
     
                  scrollPosition = GUILayout.BeginScrollView(scrollPosition,
                        GUILayout.Width(150), GUILayout.Height(150));
                 GUI.EndScrollView();
                    GUILayout.EndArea();
                    break;                  
                 case 2:            
     
                 break;            
                 case 3:            
     
               break;
                 default:            
     
                    break;      
               }

Have you tried ending the area after the switch statement?

I have an area with a scroll too and a selection grid in my code and I can move it as I use BeginArea and EndArea

Take a look at some of my code, maybe it could help!

function OnGui(){

GUILayout.BeginArea(Rect(540,220, 300, 220));
		
		scrollPosition = GUILayout.BeginScrollView (
		scrollPosition, GUILayout.Width (300), GUILayout.Height (220));
		selGridInt = GUILayout.SelectionGrid (selGridInt, arrNombresProcesos, 1);
		GUILayout.EndScrollView ();
		GUILayout.EndArea();}