Unable to solve: ArgumentException: Getting control 3's position in a group with only 3 controls when doing Repaint Aborting

Hey everyone,
Ok ok i know this was asked many times. However i wasn’t able to find any solution in the other threads.

First of all here is my editor script, or at least the important part:

public override void OnInspectorGUI()
	{
		serializedObject.Update ();
		elements = EditorGUILayout.PropertyField (serializedObject.FindProperty ("publicScenes"), false);
		serializedObject.ApplyModifiedProperties ();

		EditorGUI.indentLevel += 1;

		if (elements)
		{
			for (int count = 0; count < myScript.publicScenes.Count; ++count)
			{
				if (showPublicScenesElement.Length != myScript.publicScenes.Count)
				{
					if(Event.current.type == EventType.Repaint)
						UpdateScenesElementBools ();
				}
				else
				{
					showPublicScenesElement [count] = EditorGUILayout.Foldout (showPublicScenesElement [count], "Element " + count);
					
				}
			}
		}
		EditorGUI.indentLevel -= 1;

		if (GUILayout.Button ("Update Level buttons Animation"))
		{
			myScript.UpdateLevelAnimation ();
		}
		if (GUILayout.Button ("Recreate Buttons"))
		{
			myScript.RecreateButtons ();
		}
	}

At the beginning i had the same error but with a 9 instead of a 3 and i managed to solve part of this problem by adding the following line on line 15:

 if(Event.current.type == EventType.Repaint)

The problem persisted but now i could’t find any simple solution. I tried many things and with commenting out a lot of my code i found out that the problem is by the foldout at line 20.
Does anyone know how to get rid of this error. I am actually going crazy.

Thanks in advance
-Jorge


if (elements) {
if (showPublicScenesElement == null ||
showPublicScenesElement.Length != myScript.publicScenes.Count) {
showPublicScenesElement = new bool[myScript.publicScenes.Count];
}

    for (int count = 0; count < myScript.publicScenes.Count; ++count) {
        showPublicScenesElement[count] = EditorGUILayout.Foldout(showPublicScenesElement[count],
            "Element " + count);
    }
}
....

you should’nt update your bool “showPublicScenesElement” inside the loop.
i dont know if this will fix your problem… but just consider: what if the array lenghts of those lists are not the same, and the event called is not repaint?