Can anyone help with using EditorGUILayout.Toggle inside a loop ?

I have this code, but when I call ShowWindow with my List of strings the toggles dont let me change their values. Is anyone able to point me in the right direction ? I just want to be able to show a list with toggles and save the bool results of those toggles for use in other code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class SceneSelectorWindow : EditorWindow
{
    private static List<string> nameOfScenes = new List<string>();
    private static bool[] sceneSelections;

    public static void ShowWindow(List<string> sceneNames)
    {
        GetWindow<SceneSelectorWindow>().Show();
        nameOfScenes = sceneNames;
    }

    void OnGUI()
    {
        sceneSelections = new bool[nameOfScenes.Count];

        for (int i = 0; i < sceneSelections.Length;i++)
        {
            sceneSelections[i] = true;
        }
        float originalValue = EditorGUIUtility.labelWidth;
        EditorGUIUtility.labelWidth = 500;
        for (int i = 0; i < nameOfScenes.Count;i++)
        {
            sceneSelections[i] = EditorGUILayout.Toggle(nameOfScenes[i], sceneSelections[i]);
        }

        if (GUILayout.Button("Scan Selected Scenes"))
        {
            EditorGUIUtility.labelWidth = originalValue;
            this.Close();
        }
    }
}

You’re creating a new sceneSelections array and setting all the values to true every single time OnGUI is called.

1 Like

I wish that was the issue, but with that loop removed they are all false and still can’t be toggled on / off :frowning:

The issue appears to be trying to build the Toggles using a loop. If I explicitly create each Toggle then they work fine. But when creating them inside the loop at lines 27 thru 30 the Toggle boxes don’t work

Actually you made me test that again and it doesnt work explicitly declaring one outside of the loop either. So I think you put me on the right track sort of, At least I know its the OnGUI causing it.

Thankyou so much GroZZleR, it was line 19 causing it, once I moved that line into the ShowWindow method it started working :slight_smile:

1 Like

For anyone reading this and trying to solve the same problem here is the working code :

using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class SceneSelectorWindow : EditorWindow
{
    public static List<string> nameOfScenes = new List<string>();
    public static bool[] sceneSelections;

    public static void ShowWindow(List<string> sceneNames)
    {
        GetWindow<SceneSelectorWindow>().Show();
        nameOfScenes = sceneNames;
        sceneSelections = new bool[nameOfScenes.Count];
        for (int i = 0; i < sceneSelections.Length;i++)
        {
            sceneSelections[i] = true;
        }
    }

    void OnGUI()
    {
        float originalValue = EditorGUIUtility.labelWidth;
        EditorGUIUtility.labelWidth = 500;
        for (int i = 0; i < nameOfScenes.Count;i++)
        {
            sceneSelections[i] = EditorGUILayout.Toggle(nameOfScenes[i], sceneSelections[i]);
        }

        if (GUILayout.Button("Scan Selected Scenes"))
        {
            EditorGUIUtility.labelWidth = originalValue;
            AssetCleaner.SelectedSceneScan();
            this.Close();
        }
    }
}
1 Like