Search Unity

The infamous Getting control 0's position in a group with only 0 controls when doing Repaint

Discussion in 'Immediate Mode GUI (IMGUI)' started by IpslWon, Aug 15, 2014.

  1. IpslWon

    IpslWon

    Joined:
    Mar 1, 2013
    Posts:
    25
    First let me say I'v read up on this and through I'm still not 100% grasping the problem, I've tried the various solutions like (Event.current.type ==EventType.Repaint) and none have worked.

    What's odd is I starting this Script with a working one so I'm really at a loss.

    Code (CSharp):
    1. public class TestToolBar : MonoBehaviour {
    2.  
    3.     public bool draw = false;
    4.     public int M = 5;
    5.     public int oM = 5;
    6.     public string[] toolbarStrings = new string[] { "Black", "Blue", "Green", "Red", "White"};
    7.  
    8.     void Update()
    9.     {
    10.      
    11.         UpdateWindowSize();
    12.     }
    13.  
    14.     void UpdateWindowSize()
    15.     {
    16.         float width = Screen.width * .85f;
    17.         float height = Screen.height * .35f;;
    18.  
    19.         float posX = Screen.width * .075f;;
    20.         float posY = Screen.height * 0.65f;
    21.         windowRect = new Rect(posX, posY, width, height);
    22.     }
    23.     void OnGUI ()
    24.     {
    25.  
    26.         if (!draw)
    27.             return;
    28.         else
    29.             DrawMageButtons();
    30.     }
    31.  
    32.     void DrawMageButtons()
    33.     {
    34.             GUI.skin = thisSkin;
    35.             windowRect = GUILayout.Window(0, windowRect, drawMageSelectionTile, "", GUILayout.MinHeight(250));
    36.    
    37.     }
    38.  
    39.     void drawMageSelectionTile (int WindowID)
    40.     {
    41.         GUILayout.BeginVertical();
    42.         GUILayout.FlexibleSpace();
    43.         GUILayout.EndVertical();
    44.         GUILayout.BeginHorizontal();
    45.         M = GUILayout.Toolbar ( Mage, toolbarStrings);
    46.         GUILayout.EndHorizontal();
    47.         GUILayout.BeginVertical();
    48.         GUILayout.FlexibleSpace();
    49.         GUILayout.EndVertical();
    50.  
    51.     }
    52.  
    53.  
    54. }

    I'm at a real loss here though I'm new to the GUILayout functionality so I'm sure it's something obvious.
     
  2. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    are toolbar strings being changed?

    what line is the error coming from?
     
  3. IpslWon

    IpslWon

    Joined:
    Mar 1, 2013
    Posts:
    25
    The tool bar strings wont be changed and, after recalculating for the omitted lines, it would be line 41.

    What happens is clicking the button changes the M variable witch triggers another script.

    When I wrote this without GUILayouts, only GUI.Buttons, I had everything working and doing other GUILayouts are working fine. Just curious-er and curious-er
     
  4. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    this error happens when something was changed between "Layout" and "Repaint" events.

    OnGUI is called twice, once to find layout positions and sizes, and second to actually render them.
    If you have List/Array addition or removal logic within your OnGUI, and that logic creates or removes GUILayout elements, then you'll probably start getting these errors.

    Just make sure that the GUI element count between Layout and Repaint is the same.

    Are you using the "M" variable to populate an list of controls? or something similar?
     
  5. IpslWon

    IpslWon

    Joined:
    Mar 1, 2013
    Posts:
    25
    The number of buttons never changes, so the number of elements never changed.

    The M variable is used to populate another layout window in another script.
     
  6. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    can you post the code in that window?
     
  7. IpslWon

    IpslWon

    Joined:
    Mar 1, 2013
    Posts:
    25
    So this is called in the update function

    Code (CSharp):
    1.     void checkForCharecterChange()
    2.     {
    3.         if (Me != oM)
    4.         {  
    5.             if ( currentSelection != null)
    6.             {
    7.                 Destroy(currentSelection);  
    8.             }
    9.            
    10.             GameObject mSelected = Instantiate(mPrefabs[M], transform.position, transform.rotation) as GameObject;
    11.             oM = M;
    12.             currentSelection = mageSelected;
    13.             playerSelection.GetComponent<PlayerSelection>().playerSelection = M;
    14.         }
    15.     }
    the int player selection then uses it internally to fill out some text. That GUILayout, which I made after posting this, works fine.
     
  8. IpslWon

    IpslWon

    Joined:
    Mar 1, 2013
    Posts:
    25
    And now GUILayouts that were working are not anymore........

    I don't like to think that my code not working has something to do with an external problem, but since 4.5, it's been crashing every 20 minutes. I'd prefer to think it's me not understanding how to write layouts, but when things just magically stop working when they were working for last 2 days.... it makes me wonder.
     
  9. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    hmm, it's a little difficult to fix this problem without seeing the full context. All I know is that this error generally appears when elements change between Layout and Repaint events.
     
  10. IpslWon

    IpslWon

    Joined:
    Mar 1, 2013
    Posts:
    25
    I combined the two scripts and only one window will show at a time even though both booleans are true.

    Code (CSharp):
    1. public class MageSelectedInfo : MonoBehaviour {
    2.  
    3.     public GUISkin thisSkin;
    4.  
    5.     Rect windowRect = new Rect(20, 20, 250, 45);
    6.     public int Mage = 5;
    7.     public int oMage = 5;
    8.     public string[] toolbarStrings = new string[] { "BlackMage", "BlueMage", "GreenMage", "RedMage", "WhiteMage"};
    9.     public Object[] magePrefabs;
    10.     public GameObject playerSelection;
    11.     public GameObject currentSelection;
    12.     float heightPos;
    13.     float widthPos;
    14.  
    15.     //This is for the info screen
    16.     Rect windowRect01 = new Rect(20, 20, 250, 45);
    17.     public string mageName;
    18.     public string mageInformation;
    19.     float height01 = Screen.height / 2 - 15;
    20.     float width01 = Screen.width / 2.5f;
    21.     float posX01;
    22.     float posY01;
    23.  
    24.     bool MageButtonsGUI;
    25.     bool MageInfoGUI;
    26.  
    27.     float offestY;
    28.     float offestZ;
    29.  
    30.     MageSelectionVariables mageVars;
    31.     MageTextInfo mageText;
    32.  
    33.     // Use this for initialization
    34.     void Start ()
    35.     {
    36.         Intialize();
    37.     }
    38.  
    39.     void Intialize()
    40.     {
    41.         posX01 = Screen.width * 0.2f;
    42.         posY01 = height01/2 + 20;
    43.      
    44.         mageVars = GetComponent<MageSelectionVariables>();
    45.         mageText = GetComponent<MageTextInfo>();
    46.  
    47.  
    48.         //this is for the toolbar window that has 5 buttons
    49.         heightPos = (Screen.height * .75f);
    50.         widthPos = (Screen.width * .5f)-225;
    51.         magePrefabs = Resources.LoadAll("MagePrefabs", typeof(GameObject));
    52.         playerSelection = GameObject.FindGameObjectWithTag("DnD");
    53.      
    54.     }
    55.  
    56.  
    57.     // Update is called once per frame
    58.     void Update ()
    59.     {
    60.         checkForEnable();
    61.      
    62.         checkForCharecterChange();
    63.         UpdateToolbarWindow();
    64.  
    65.         checkMageColor();
    66.         upDateInfoWindow();
    67.         checkCurMage();
    68.     }
    69.  
    70.     void checkForEnable()
    71.     {
    72.         if (mageVars.CurrentProgression == MageSelectionVariables.Progression.creatingCharecter)
    73.             MageButtonsGUI = true;
    74.     }
    75.  
    76.     void checkMageColor(){
    77.  
    78.         if (Mage == 0){mageInformation = mageText.blackMage; }
    79.         if (Mage == 1){mageInformation = mageText.blueMage; }
    80.         if (Mage == 2){mageInformation = mageText.greenMage; }
    81.         if (Mage == 3){mageInformation = mageText.redMage; }
    82.         if (Mage == 4){mageInformation = mageText.whiteMage; }
    83.  
    84.     }
    85.  
    86.     void checkCurMage()
    87.     {
    88.         if (currentSelection != null)
    89.         {
    90.             GameObject curMage = currentSelection;
    91.          
    92.             mageName = curMage.GetComponent<CommonName>().ThisCommonName;
    93.             mageVars.defaultMaxHealth = curMage.GetComponent<Health>().health;
    94.             mageVars.defaultMaxMana = curMage.GetComponent<PlayerInfo>().maxMana;
    95.             mageVars.minB_Damage = curMage.GetComponent<PlayerInfo>().minBonus_Damage;
    96.             mageVars.maxB_Damage = curMage.GetComponent<PlayerInfo>().maxBonus_Damage;
    97.             mageVars.defaultCriticalChance = curMage.GetComponent<PlayerInfo>().Critical_Chance;
    98.             mageVars.defaultCriticalBonus = curMage.GetComponent<PlayerInfo>().Critical_Bonus;
    99.             mageVars.defaultCasting_Speed_Bonus = curMage.GetComponent<PlayerInfo>().Casting_Speed_Bonus;
    100.             mageVars.defaultGathering_Bonus = curMage.GetComponent<PlayerInfo>().Gathering_Bonus;
    101.             mageVars.defaultConcentration = curMage.GetComponent<PlayerInfo>().Casting_Concentration;
    102.  
    103.             MageInfoGUI = true;
    104.         }
    105.  
    106.  
    107.     }
    108.  
    109.     void upDateInfoWindow()
    110.     {
    111.         height01 = Screen.height * 0.55f;
    112.         width01 = Screen.width * 0.55f;
    113.         posX01 =  Screen.width * 0.45f;
    114.         posY01 = Screen.height * 0.0005f;
    115.      
    116.         windowRect01 = new Rect(posX01, posY01, width01, height01);
    117.  
    118.     }
    119.  
    120.     void UpdateToolbarWindow()
    121.     {
    122.         float width = Screen.width * .85f;
    123.         float height = Screen.height * .35f;;
    124.      
    125.         float posX = Screen.width * .075f;;
    126.         float posY = Screen.height * 0.65f;
    127.         windowRect = new Rect(posX, posY, width, height);
    128.     }
    129.  
    130.  
    131.     void checkForCharecterChange()
    132.     {
    133.         if (Mage != oMage)
    134.         {  
    135.             if ( currentSelection != null)
    136.             {
    137.                 Destroy(currentSelection);  
    138.             }
    139.          
    140.             GameObject mageSelected = Instantiate(magePrefabs[Mage], transform.position, transform.rotation) as GameObject;
    141.             oMage = Mage;
    142.             currentSelection = mageSelected;
    143.         }
    144.     }
    145.  
    146.     void OnGUI()
    147.     {
    148.         GUI.skin = thisSkin;
    149.         if (MageButtonsGUI)
    150.             DrawMageButtons();
    151.             //now adjust to the group. (0,0) is the topleft corner of the group.
    152.             GUI.BeginGroup (new Rect (0,0,100,100));
    153.  
    154.             // End the group we started above. This is very important to remember!
    155.             GUI.EndGroup ();
    156.  
    157.         if (MageInfoGUI)
    158.             DrawInfoScreen();
    159.             //now adjust to the group. (0,0) is the topleft corner of the group.
    160.             GUI.BeginGroup (new Rect (0,0,100,100));
    161.             // End the group we started above. This is very important to remember!
    162.             GUI.EndGroup ();
    163.          
    164.  
    165.     }
    166.  
    167.     #region this is the group for the Mage toolbar on lower third of the screen
    168.     void DrawMageButtons()
    169.     {
    170.         windowRect = GUILayout.Window(0, windowRect, drawMageSelectionTile, "", GUILayout.MinHeight(250));
    171.     }
    172.  
    173.     void drawMageSelectionTile (int WindowID)
    174.     {
    175.         GUILayout.BeginVertical();
    176.         GUILayout.FlexibleSpace();
    177.         //GUILayout.EndVertical();
    178.      
    179.         GUILayout.BeginHorizontal();
    180.         Mage = GUILayout.Toolbar ( Mage, toolbarStrings);
    181.         GUILayout.EndHorizontal();
    182.      
    183.         //GUILayout.BeginVertical();
    184.         GUILayout.FlexibleSpace();
    185.         GUILayout.EndVertical();
    186.      
    187.     }
    188.     #endregion
    189.  
    190.     #region this is the group for the Info screen on the right side
    191.  
    192.     void DrawInfoScreen()
    193.     {
    194.         windowRect01 = GUILayout.Window(0, windowRect01, drawMageSelectedInfo, "", GUILayout.MinHeight(250));
    195.     }
    196.  
    197.  
    198.     void drawMageSelectedInfo(int WindowID)
    199.     {  
    200.             GUILayout.BeginVertical();
    201.             GUILayout.Space(40);
    202.             //GUILayout.Space(8);
    203.             GUILayout.BeginHorizontal();
    204.             GUILayout.FlexibleSpace();
    205.             GUILayout.Label("" + mageName , "LegendaryText");
    206.             GUILayout.FlexibleSpace();
    207.             GUILayout.EndHorizontal();
    208.  
    209.             GUILayout.BeginHorizontal();
    210.             GUILayout.Label("Health: " + mageVars.defaultMaxHealth + " " , "LegendaryText");
    211.             GUILayout.FlexibleSpace();
    212.             GUILayout.Label("Mana: " + mageVars.defaultMaxMana + " ", "LegendaryText");
    213.             GUILayout.FlexibleSpace();
    214.             GUILayout.EndHorizontal();
    215.  
    216.             GUILayout.BeginHorizontal();
    217.             GUILayout.Label("Critical Chance: " + mageVars.defaultCriticalChance + "%", "LegendaryText");
    218.             GUILayout.FlexibleSpace();
    219.             GUILayout.Label( "Critical Bonus: " + mageVars.defaultCriticalBonus + "%", "LegendaryText");
    220.             GUILayout.FlexibleSpace();
    221.             GUILayout.EndHorizontal();
    222.  
    223.             GUILayout.BeginHorizontal();
    224.             GUILayout.Label("Bonus Damamage: " + mageVars.minB_Damage + "-" + mageVars.maxB_Damage, "LegendaryText");
    225.             GUILayout.FlexibleSpace();
    226.             GUILayout.Label( "Concentration Bouns  " + mageVars.defaultConcentration, "LegendaryText");
    227.             GUILayout.FlexibleSpace();
    228.             GUILayout.EndHorizontal();
    229.  
    230.             GUILayout.BeginHorizontal();
    231.             GUILayout.Label("Casting Bonus: " + mageVars.defaultCasting_Speed_Bonus, "LegendaryText");
    232.             GUILayout.FlexibleSpace();
    233.             GUILayout.Label( "Gathering Bonus: " + mageVars.defaultGathering_Bonus, "LegendaryText");
    234.             GUILayout.FlexibleSpace();
    235.             GUILayout.EndHorizontal();
    236.  
    237.             GUILayout.BeginVertical();
    238.             GUILayout.FlexibleSpace();
    239.             GUILayout.Label( "   " + mageInformation , "LegendaryText");
    240.             GUILayout.FlexibleSpace();
    241.             GUILayout.EndVertical();
    242.  
    243.             GUILayout.EndVertical();
    244.         }
    245.     #endregion
    246.  
    247. }
    248.  
     
  11. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    Looks ok to me. Unless somehow "MageButtonsGUI" or "MageInfoGUI" is set to false somewhere in your GUI code.
     
  12. IpslWon

    IpslWon

    Joined:
    Mar 1, 2013
    Posts:
    25
    That's the whole thing, I made them public at first just to see!!!

    It's beyond frustrating and really since this new update, I can't get anything done. I wonder if it's me or if its just a bug in the newest update.
     
  13. IpslWon

    IpslWon

    Joined:
    Mar 1, 2013
    Posts:
    25
    The problem, which took me till just now was right here:
    Code (CSharp):
    1.     void DrawMageButtons()
    2.     {
    3.         windowRect = GUILayout.Window(0, windowRect, drawMageSelectionTile, "", GUILayout.MinHeight(250));
    4.     }
    5.      void DrawInfoScreen()
    6.     {
    7.         windowRect01 = GUILayout.Window(0, windowRect01, drawMageSelectedInfo, "", GUILayout.MinHeight(250));
    8.     }
    you can see they are oth bringing in the same window int ID of 0, so only one can be 0.

    That still hasn't addressed my original paint problem, but I'm just removing some stuff to get back to working out code as oppose to jumping back into this layout business.

    It seems that the proble is where I add BeginVeritical or BeginHorizontal spaces in for loops
     
    Last edited: Aug 16, 2014