ArgumentException: Getting control 0's position in a group with only 0 controls when doing Repaint A

 
   void OnGUI () { 
   GUI.depth = 2; 
   if (Guib){ 
   GUI.skin = GuiSkin; 
   GuiF(); 
   } 
    
   } 
    
   void GuiF(){ 
   GUI.Box(new Rect(0,0,50,50),"dsfg"); 
   GUILayout.BeginArea(new Rect(0,0,Screen.width,Screen.height)); 
   GUILayout.Box("I'm Name"); 
   GUILayout.Box("I'm Health"); 
   GUILayout.Box("I'm Stamina"); 
   GUILayout.Box("I'm Nutrition"); 
   GUILayout.Box("I'm fOOd"); 
   GUILayout.Box("I'm Water"); 
   GUILayout.Box("I'm Bladder S**T"); 
   GUILayout.Box("I'm Bladder water"); 
   GUILayout.Box("I'll get back to this later"); 
   GUILayout.Box("tho I think that's all I need"); 
   GUILayout.EndArea(); 
   } 
  


hat's quite simple. The reason for this error is always the same. You changed the content of your GUI between the Layout and Repaint event. OnGUI is called for many different events (See the EventType enum for the full list).

Before each event Unity calls an additional Layout event. In this event Unity collects the information how many elements you have at the moment and hoe they are nested. This information is needed by the Layouting system.

When the actual event is processed it's vital that OnGUI has the same content as it has when the Layout event was called. That's why you shouldn't place any logic that changes the state of your GUI inside OnGUI. If you want place it in OnGUI, wrap it in

 
  
  1. if (Event.current.type == EventType.Repaint)
  2. {
  3. //
  4. }
  5. When you change the state **at the end** of OnGUI, so the elements aren't affected since they are already drawn.

Or use

 
  
  1. if (Event.current.type == EventType.Layout)
  2. {
  3. //
  4. }

when you place your code before the actual GUI elements.

Anyway in general it's better to put any logic in Update.

edit
On this question i've posted a quite detailed post how the GUI system works if you want to gain some more knowledge about Unity ;).


[Solved]ArgumentException Getting control..

  1. Shintaja

    Shintaja

    Joined:
    Jan 13, 2012
    Posts:
    10
    If I don't call GUI functions in OnInspectorGUI it will result in an ArgumentException. This is very bizarre behaviour and it results in that I cannot use the custom editor at all.
    Code (csharp):
    1. using UnityEngine;
    2. using System. Collections;
    3.  
    4. public class RandomClass : MonoBehaviour
    5. {
    6.  
    7.     public bool asd = false;
    8.  
    9. }
    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System. Collections;
    4.  
    5. [CanEditMultipleObjects]
    6. [CustomEditor ( typeof(RandomClass))]
    7. public class RandomClassEditor : Editor
    8. {
    9.     private RandomClass s
    10.     {
    11.         get { return target as RandomClass; }
    12.     }
    13.  
    14.     public override void OnInspectorGUI ()
    15.     {
    16.         if ( s. asd )
    17.             EditorGUILayout. Space (); // Error is given here.
    18.  
    19.         s. asd = true;
    20.     }
    21. }
    ArgumentException: Getting control 0's position in a group with only 0 controls when doing Repaint
    Aborting
    UnityEngine.GUILayoutGroup.GetNext () (at C:/buildslave/unity/build/Runtime/IMGUI/Managed/GUILayoutUtility.cs:665)
    UnityEngine.GUILayoutUtility.DoGetRect (Single minWidth, Single maxWidth, Single minHeight, Single maxHeight, UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options) (at C:/buildslave/unity/build/Runtime/IMGUI/Managed/GUILayoutUtility.cs:410)
    UnityEngine.GUILayoutUtility.GetRect (Single width, Single height) (at C:/buildslave/unity/build/Runtime/IMGUI/Managed/GUILayoutUtility.cs:377)
    UnityEditor.EditorGUILayout.Space () (at C:/buildslave/unity/build/Editor/Mono/EditorGUI.cs:6864)
    InterfaceEditor.OnInspectorGUI () (at Assets/Scripts/Interface/Editor/RandomClassEditor.cs:27)
    UnityEditor.InspectorWindow.DrawEditor (UnityEditor.Editor editor, Int32 editorIndex, Boolean rebuildOptimizedGUIBlock, System.Boolean& showImportedObjectBarNext, UnityEngine.Rect& importedObjectBarRect) (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1232)
    UnityEditor.DockArea:OnGUI()
    Click to expand...
     
    Last edited: Apr 6, 2016
    Shintaja,Apr 6, 2016
    #1
  2. Durazell

    Durazell

    Joined:
    Sep 28, 2013
    Posts:
    9
    Code (CSharp):
    1. bool layoutDone = false;
    2. public override void OnInspectorGUI ()
    3. {
    4.     if (s. asd)
    5.     {
    6.         if (( Event. current. type == EventType. Repaint && layoutDone == true) ||
    7.               Event. current. type == EventType. Layout)
    8.         {
    9.             EditorGUILayout. Space();
    10.         }
    11.         if ( Event. current. type == EventType. Layout)
    12.         {        
    13.              layoutDone = true;
    14.         }
    15.      }
    16.  
    17.     s. asd = true;
    18. }
    I think the first time s.asd is true Event is Repaint and not Layout, which causes error.
     
    Last edited: Apr 6, 2016
    Durazell,Apr 6, 2016
    #2
  3. Shintaja

    Shintaja

    Joined:
    Jan 13, 2012
    Posts:
    10
    Thanks, it seems to be working. I did come across the Events while googling and was just testing them as well, I wish it was mentioned in the script reference of OnInspectorGUI.
     

你可能感兴趣的:(unity3d)