Search Unity

Editor window bool value changes to false when I hit play

Discussion in 'Scripting' started by Kobaltic1, Jan 28, 2021.

  1. Kobaltic1

    Kobaltic1

    Joined:
    Jan 22, 2015
    Posts:
    183
    I have a editor window. I have a bool value as a toggle. I can change the value to true and close the window. If I reopen the window it is still true. However when I click play, the value changes to false. I have even left the window open and watched it change to false. I don't know how to keep the value true.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public class KobieWindow : EditorWindow
    5. {
    6.  
    7.     public static bool myBool;  
    8.  
    9.     // Add menu named "My Window" to the Window menu
    10.     [MenuItem("Window/Kobie Window")]
    11.     static void Init()
    12.     {
    13.         // Get existing open window or if none, make a new one:
    14.         KobieWindow window = (KobieWindow)EditorWindow.GetWindow(typeof(KobieWindow));
    15.         window.Show();
    16.     }
    17.  
    18.     void OnGUI()
    19.     {
    20.         GUILayout.Label("Play Mode Settings", EditorStyles.boldLabel);
    21.         myBool = EditorGUILayout.Toggle("Load Lobby Manager", myBool);
    22.      
    23.     }
    24. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    First it is static so it cannot be serialized.

    Second, to persist non-static values, use Undo.RecordObject(). See docs for details of using that method.