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

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.

public class TestToolBar : MonoBehaviour {

    public bool draw = false;
    public int M = 5;
    public int oM = 5;
    public string[] toolbarStrings = new string[] { "Black", "Blue", "Green", "Red", "White"};

    void Update()
    {
      
        UpdateWindowSize();
    }

    void UpdateWindowSize()
    {
        float width = Screen.width * .85f;
        float height = Screen.height * .35f;;

        float posX = Screen.width * .075f;;
        float posY = Screen.height * 0.65f;
        windowRect = new Rect(posX, posY, width, height);
    }
    void OnGUI ()
    {

        if (!draw)
            return;
        else
            DrawMageButtons();
    }

    void DrawMageButtons()
    {
            GUI.skin = thisSkin;
            windowRect = GUILayout.Window(0, windowRect, drawMageSelectionTile, "", GUILayout.MinHeight(250));
    
    }

    void drawMageSelectionTile (int WindowID)
    {
        GUILayout.BeginVertical();
        GUILayout.FlexibleSpace();
        GUILayout.EndVertical();
        GUILayout.BeginHorizontal();
        M = GUILayout.Toolbar ( Mage, toolbarStrings);
        GUILayout.EndHorizontal();
        GUILayout.BeginVertical();
        GUILayout.FlexibleSpace();
        GUILayout.EndVertical();

    }


}

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

are toolbar strings being changed?

what line is the error coming from?

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

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?

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.

can you post the code in that window?

So this is called in the update function

    void checkForCharecterChange()
    {
        if (Me != oM)
        {   
            if ( currentSelection != null)
            {
                Destroy(currentSelection);   
            }
           
            GameObject mSelected = Instantiate(mPrefabs[M], transform.position, transform.rotation) as GameObject;
            oM = M;
            currentSelection = mageSelected;
            playerSelection.GetComponent<PlayerSelection>().playerSelection = M;
        }
    }

the int player selection then uses it internally to fill out some text. That GUILayout, which I made after posting this, works fine.

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.

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.

I combined the two scripts and only one window will show at a time even though both booleans are true.

public class MageSelectedInfo : MonoBehaviour {

    public GUISkin thisSkin;

    Rect windowRect = new Rect(20, 20, 250, 45);
    public int Mage = 5;
    public int oMage = 5;
    public string[] toolbarStrings = new string[] { "BlackMage", "BlueMage", "GreenMage", "RedMage", "WhiteMage"};
    public Object[] magePrefabs;
    public GameObject playerSelection;
    public GameObject currentSelection;
    float heightPos;
    float widthPos;

    //This is for the info screen
    Rect windowRect01 = new Rect(20, 20, 250, 45);
    public string mageName;
    public string mageInformation;
    float height01 = Screen.height / 2 - 15;
    float width01 = Screen.width / 2.5f;
    float posX01;
    float posY01;

    bool MageButtonsGUI;
    bool MageInfoGUI;

    float offestY;
    float offestZ;

    MageSelectionVariables mageVars;
    MageTextInfo mageText;

    // Use this for initialization
    void Start ()
    {
        Intialize();
    }

    void Intialize()
    {
        posX01 = Screen.width * 0.2f;
        posY01 = height01/2 + 20;
      
        mageVars = GetComponent<MageSelectionVariables>();
        mageText = GetComponent<MageTextInfo>();


        //this is for the toolbar window that has 5 buttons
        heightPos = (Screen.height * .75f);
        widthPos = (Screen.width * .5f)-225;
        magePrefabs = Resources.LoadAll("MagePrefabs", typeof(GameObject));
        playerSelection = GameObject.FindGameObjectWithTag("DnD");
      
    }

  
    // Update is called once per frame
    void Update ()
    {
        checkForEnable();
      
        checkForCharecterChange();
        UpdateToolbarWindow();

        checkMageColor();
        upDateInfoWindow();
        checkCurMage();
    }

    void checkForEnable()
    {
        if (mageVars.CurrentProgression == MageSelectionVariables.Progression.creatingCharecter)
            MageButtonsGUI = true;
    }

    void checkMageColor(){

        if (Mage == 0){mageInformation = mageText.blackMage; }
        if (Mage == 1){mageInformation = mageText.blueMage; }
        if (Mage == 2){mageInformation = mageText.greenMage; }
        if (Mage == 3){mageInformation = mageText.redMage; }
        if (Mage == 4){mageInformation = mageText.whiteMage; }

    }

    void checkCurMage()
    {
        if (currentSelection != null)
        {
            GameObject curMage = currentSelection;
          
            mageName = curMage.GetComponent<CommonName>().ThisCommonName;
            mageVars.defaultMaxHealth = curMage.GetComponent<Health>().health;
            mageVars.defaultMaxMana = curMage.GetComponent<PlayerInfo>().maxMana;
            mageVars.minB_Damage = curMage.GetComponent<PlayerInfo>().minBonus_Damage;
            mageVars.maxB_Damage = curMage.GetComponent<PlayerInfo>().maxBonus_Damage;
            mageVars.defaultCriticalChance = curMage.GetComponent<PlayerInfo>().Critical_Chance;
            mageVars.defaultCriticalBonus = curMage.GetComponent<PlayerInfo>().Critical_Bonus;
            mageVars.defaultCasting_Speed_Bonus = curMage.GetComponent<PlayerInfo>().Casting_Speed_Bonus;
            mageVars.defaultGathering_Bonus = curMage.GetComponent<PlayerInfo>().Gathering_Bonus;
            mageVars.defaultConcentration = curMage.GetComponent<PlayerInfo>().Casting_Concentration;

            MageInfoGUI = true;
        }


    }

    void upDateInfoWindow()
    {
        height01 = Screen.height * 0.55f;
        width01 = Screen.width * 0.55f;
        posX01 =  Screen.width * 0.45f;
        posY01 = Screen.height * 0.0005f;
      
        windowRect01 = new Rect(posX01, posY01, width01, height01);

    }

    void UpdateToolbarWindow()
    {
        float width = Screen.width * .85f;
        float height = Screen.height * .35f;;
      
        float posX = Screen.width * .075f;;
        float posY = Screen.height * 0.65f;
        windowRect = new Rect(posX, posY, width, height);
    }


    void checkForCharecterChange()
    {
        if (Mage != oMage)
        {  
            if ( currentSelection != null)
            {
                Destroy(currentSelection);  
            }
          
            GameObject mageSelected = Instantiate(magePrefabs[Mage], transform.position, transform.rotation) as GameObject;
            oMage = Mage;
            currentSelection = mageSelected;
        }
    }

    void OnGUI()
    {
        GUI.skin = thisSkin;
        if (MageButtonsGUI)
            DrawMageButtons();
            //now adjust to the group. (0,0) is the topleft corner of the group.
            GUI.BeginGroup (new Rect (0,0,100,100));

            // End the group we started above. This is very important to remember!
            GUI.EndGroup ();

        if (MageInfoGUI)
            DrawInfoScreen();
            //now adjust to the group. (0,0) is the topleft corner of the group.
            GUI.BeginGroup (new Rect (0,0,100,100));
            // End the group we started above. This is very important to remember!
            GUI.EndGroup ();
          

    }

    #region this is the group for the Mage toolbar on lower third of the screen
    void DrawMageButtons()
    {
        windowRect = GUILayout.Window(0, windowRect, drawMageSelectionTile, "", GUILayout.MinHeight(250));
    }
  
    void drawMageSelectionTile (int WindowID)
    {
        GUILayout.BeginVertical();
        GUILayout.FlexibleSpace();
        //GUILayout.EndVertical();
      
        GUILayout.BeginHorizontal();
        Mage = GUILayout.Toolbar ( Mage, toolbarStrings);
        GUILayout.EndHorizontal();
      
        //GUILayout.BeginVertical();
        GUILayout.FlexibleSpace();
        GUILayout.EndVertical();
      
    }
    #endregion

    #region this is the group for the Info screen on the right side

    void DrawInfoScreen()
    {
        windowRect01 = GUILayout.Window(0, windowRect01, drawMageSelectedInfo, "", GUILayout.MinHeight(250));
    }


    void drawMageSelectedInfo(int WindowID)
    {  
            GUILayout.BeginVertical();
            GUILayout.Space(40);
            //GUILayout.Space(8);
            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            GUILayout.Label("" + mageName , "LegendaryText");
            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label("Health: " + mageVars.defaultMaxHealth + " " , "LegendaryText");
            GUILayout.FlexibleSpace();
            GUILayout.Label("Mana: " + mageVars.defaultMaxMana + " ", "LegendaryText");
            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label("Critical Chance: " + mageVars.defaultCriticalChance + "%", "LegendaryText");
            GUILayout.FlexibleSpace();
            GUILayout.Label( "Critical Bonus: " + mageVars.defaultCriticalBonus + "%", "LegendaryText");
            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label("Bonus Damamage: " + mageVars.minB_Damage + "-" + mageVars.maxB_Damage, "LegendaryText");
            GUILayout.FlexibleSpace();
            GUILayout.Label( "Concentration Bouns  " + mageVars.defaultConcentration, "LegendaryText");
            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label("Casting Bonus: " + mageVars.defaultCasting_Speed_Bonus, "LegendaryText");
            GUILayout.FlexibleSpace();
            GUILayout.Label( "Gathering Bonus: " + mageVars.defaultGathering_Bonus, "LegendaryText");
            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();

            GUILayout.BeginVertical();
            GUILayout.FlexibleSpace();
            GUILayout.Label( "   " + mageInformation , "LegendaryText");
            GUILayout.FlexibleSpace();
            GUILayout.EndVertical();

            GUILayout.EndVertical();
        }
    #endregion
  
}

Looks ok to me. Unless somehow “MageButtonsGUI” or “MageInfoGUI” is set to false somewhere in your GUI code.

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.

The problem, which took me till just now was right here:

    void DrawMageButtons()
    {
        windowRect = GUILayout.Window(0, windowRect, drawMageSelectionTile, "", GUILayout.MinHeight(250));
    }
     void DrawInfoScreen()
    {
        windowRect01 = GUILayout.Window(0, windowRect01, drawMageSelectedInfo, "", GUILayout.MinHeight(250));
    }

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