OnInspectorGUI() Getting Control 2's position in a group with only 2 controls when doing repaint

Hello guys,

Before I am going to ask help from you guys I am going to say that I have searched carefully on the internet, and yes, I have googled it before asking this. I have encountered pages like this one or this one.

I still couldn’t fix it and it really is frustrating me. My problem is that I am getting an error

ArgumentException: Getting control 2's position in a group with only 2 controls when doing repaint
Aborting

And the ‘2’s’ always changes from numbers, so it is sometimes 0’s or 1’s.
This error occurred out of the sudden and the inspector is still working without any bugs.

Using Event.current.type == EventType.Layout or Event.current.type == EventType.Repaint really didn’t work out for me. But I might have not used it at the right place.

Here is my code:

using UnityEngine;
using UnityEditor;
using Utilities;
using GenerationType = HeightMapGenerator.GenerationType;
// ReSharper disable All

[CustomEditor(typeof(HeightMapGenerator))]
public class HeightMapGeneratorEditor : Editor
{
    private HeightMapGenerator _HeightMapGenerator;

    private Texture2D _HeightMapTexture2D;
    private float _Scaler;

    private bool _Preview;

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        if (_HeightMapGenerator == null)
            _HeightMapGenerator = target as HeightMapGenerator;
        else
        {
            if (_HeightMapGenerator.GetGenerationType == GenerationType.PerlinNoise)
            {
                bool valueHasChanged = CreatePerlinNoiseWindow();

                Utilties.Canvas.UseButtonForPreview(ref _Preview, "Preview HeightMap", "Remove Preview");

                if (_Preview == true)
                    CreatePerlinMapPreview(generateNewHeightmap: valueHasChanged);
                else
                    _HeightMapTexture2D = null;

                _HeightMapGenerator.SetScaler = _Scaler;
            }


            if (_HeightMapGenerator.GetGenerationType == GenerationType.FallOffMap)
            {
                CreateFallOfMapWindow();
            }
        }
    }

    private void CreatePerlinMapPreview(bool generateNewHeightmap)
    {
        //Generate first the heightmap.
        if (generateNewHeightmap || _HeightMapTexture2D == null)
        {
            _HeightMapGenerator.GenerateHeightMap();

            _HeightMapTexture2D = Utilties.HeightMap.GenerateHeightmapTexture2D(
                _HeightMapGenerator.GetHeightMapArray,
                HeightMapGenerator.PerlineMapWidth,
                HeightMapGenerator.PerlineMapHeigth);
        }

        //View the heightmap on the inspector.
        if (_HeightMapTexture2D != null)
        {
            Utilties.Canvas.PreviewTexture2D(_HeightMapTexture2D);
        }
    }

    /// <summary>
    /// Create Editor window for the Perlin Noise Hightmap Tool.
    /// <para> Return true if some values has changed in on the inspector. </para>
    /// </summary>
    /// <returns></returns>
    private bool CreatePerlinNoiseWindow()
    {
        EditorGUI.BeginChangeCheck();

        EditorGUILayout.LabelField("HeightMap Options:");

        EditorGUILayout.BeginHorizontal();

        EditorGUILayout.LabelField("Scaler");
        _Scaler = EditorGUILayout.Slider(_Scaler, 0f, 100f);

        EditorGUILayout.EndHorizontal();

        if (EditorGUI.EndChangeCheck())
            return true;

        return false;
    }

    private void CreateFallOfMapWindow()
    {
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Texture");
        GUILayout.FlexibleSpace();

        Texture2D fallOffMapTexture2D = null;
        fallOffMapTexture2D = (Texture2D)EditorGUILayout.ObjectField(fallOffMapTexture2D, typeof(Texture2D), allowSceneObjects: false,
           options: new GUILayoutOption[] { GUILayout.Width(100), GUILayout.Height(100) });
        _HeightMapGenerator.FallOffMap = fallOffMapTexture2D;

        GUILayout.FlexibleSpace();
        EditorGUILayout.EndHorizontal();
    }
}

And the code for the utilities class:

 public static class Canvas
        {
            public static void UseButtonForPreview(ref bool previewBool, string textNoPreview, string textOnPreview)
            {
                if (GUILayout.Button((previewBool == false) ? textNoPreview : textOnPreview))
                {
                    previewBool = !previewBool;
                }
            }

            public static void PreviewTexture2D(Texture2D heightMap)
            {
                Rect textureRect = GUILayoutUtility.GetRect(250f, 250f, GUILayout.ExpandWidth(false),
                    GUILayout.ExpandHeight(false));

                textureRect.x = EditorGUIUtility.currentViewWidth - textureRect.width - 50;
                EditorGUI.DrawPreviewTexture(textureRect, heightMap);
                EditorGUILayout.Space();
            }
        }

I am using the latest Unity version of 2017 which is 2017.1.03f.

I fixed it!

I changed the code:

 public override void OnInspectorGUI()
 {
     base.OnInspectorGUI();
 
     if (_HeightMapGenerator == null)
         _HeightMapGenerator = target as HeightMapGenerator;
     else
     {
         if (_HeightMapGenerator.GetGenerationType == GenerationType.PerlinNoise)
         {
             bool valueHasChanged = CreatePerlinNoiseWindow();
             ........

To this

 public override void OnInspectorGUI()
 {
     base.OnInspectorGUI();
 
     if (_HeightMapGenerator == null)
         _HeightMapGenerator = target as HeightMapGenerator;

     if (_HeightMapGenerator.GetGenerationType == GenerationType.PerlinNoise)
     {
          bool valueHasChanged = CreatePerlinNoiseWindow();
          ........

As you can see, I have removed the else statement after the if (_HeightMapGenerator == null) statement.

I am not sure how this has fixed the issue, but my theory is that the Editor’s Event state is Layout till it gets to the first if statement. And it didn’t execute the rest of the code because the rest of the code was in the else statement of this first if statement.
The rest of the code in the else statement would have been executed when this Event state was Repaint.

I’d really appreciate an explanation to this fix since it is not really clear for me.

Damn, I have exact same issue.
Thank you very much for providing a part of your “old” and “fixed” code parts.