Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Variable Changes Not Saving

Discussion in 'Immediate Mode GUI (IMGUI)' started by BrandonK, Mar 18, 2016.

  1. BrandonK

    BrandonK

    Joined:
    Sep 18, 2015
    Posts:
    41
    I'm about to go insane PLEASE can someone help me. I'm trying to make a simple editor script (And I admit I'm not amazing at editor scripting but I think this is pretty simple), the problem is that after I've made changes to it they reset (as in the list goes back to null) when playing or editing the scripts. I've searched on the internet and the only answer I've found is using SetDirty() which does nothing. Here are my scripts.

    LevelHandlerEditor.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. [CustomEditor(typeof(LevelHandler))]
    7. public class LevelHandlerEditor : Editor {
    8.    
    9.     public override void OnInspectorGUI()
    10.     {
    11.         LevelHandler lh = (LevelHandler)target;
    12.        
    13.         if (lh.levels == null) {
    14.             lh.levels = new List<Level>();
    15.         }
    16.        
    17.         /// Add / Remove Levels (Buttons) ///
    18.         EditorGUILayout.BeginHorizontal();
    19.         if (GUILayout.Button("Remove"))
    20.             lh.levels.RemoveAt(lh.levels.Count - 1);
    21.         if (GUILayout.Button("Add")) {
    22.             lh.levels.Add(new Level());
    23.             lh.levels[lh.levels.Count - 1].sceneName = "Level " + lh.levels.Count;
    24.         }
    25.         EditorGUILayout.EndHorizontal();
    26.         ///
    27.        
    28.         foreach(Level l in lh.levels) {
    29.             EditorGUILayout.BeginHorizontal();
    30.             l.editorToggle = EditorGUILayout.Foldout(l.editorToggle, "");
    31.             //GUILayout.Space(-100f);
    32.             l.sceneName = EditorGUILayout.TextField(l.sceneName);
    33.             EditorGUILayout.EndHorizontal();
    34.            
    35.             if (l.editorToggle) {
    36.                 EditorGUI.indentLevel++;
    37.                 l.scene = (GameObject) EditorGUILayout.ObjectField("Scene", l.scene, typeof(object), true);
    38.                
    39.                 /// Stars ///
    40.                 l.editorStarToggle = EditorGUILayout.Foldout(l.editorStarToggle, "Star Requirements");
    41.                 if (l.editorStarToggle) {
    42.                     EditorGUI.indentLevel++;
    43.                    
    44.                     for (int i = 0; i < 3; i++) {
    45.                         l.stars[i] = EditorGUILayout.IntField("star " + (i + 1).ToString(), l.stars[i]);
    46.                     }
    47.                    
    48.                     EditorGUI.indentLevel--;
    49.                 }
    50.                 ///
    51.                
    52.                 EditorGUILayout.Space();
    53.                
    54.                 l.cameraOffset = EditorGUILayout.Vector3Field("Camera Offset", l.cameraOffset);
    55.                
    56.                 EditorGUI.indentLevel--;
    57.             }
    58.         }
    59.        
    60.         EditorGUILayout.Separator();
    61.         if (GUILayout.Button("Save Changes"))
    62.             EditorUtility.SetDirty(lh);
    63.     }
    64. }
    LevelHandler.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class LevelHandler : MonoBehaviour{
    6.     public List<Level> levels;
    7. }
    8.  
    9. public class Level {
    10.     public string sceneName;
    11.     public GameObject scene;
    12.     public int[] stars = new int[3];
    13.     public Vector3 cameraOffset;
    14.    
    15.     // Editor Variables
    16.     public bool editorToggle;
    17.     public bool editorStarToggle;
    18. }
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,850
  3. BrandonK

    BrandonK

    Joined:
    Sep 18, 2015
    Posts:
    41
    Thank You! It works although only if it's not derived of monobehaviour is there any way to do this whilst having it derived of monobehaviour?
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,850
    What do you mean? Can you show your code?
     
  5. BrandonK

    BrandonK

    Joined:
    Sep 18, 2015
    Posts:
    41
    I mean this:
    Code (CSharp):
    1. [System.Serializable]
    2. public class LevelHandler {
    3.     public List<Level> levels;
    4. }
    works, but this:
    Code (CSharp):
    1. [System.Serializable]
    2. public class LevelHandler : MonoBehaviour {
    3.     public List<Level> levels;
    4. }
    doesn't.

    But with the first example I get an error in my editor script because of this line of code
    Code (CSharp):
    1. LevelHandler lh = (LevelHandler) target;
    because it can't cast from LevelHandler to Object.

    this would require me to redo my entire editor script if I'm correct (if I have to I will) but I would also like to add additional functionality to the LevelHandler class which would require it to be derived of monoDevelop.
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,850
    The System.Serializable should go above the Level class, not LevelHandler. LevelHandler should inherit from MonoBehaviour.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. public class LevelHandler : MonoBehaviour{
    5.     public List<Level> levels;
    6. }
    7.  
    8. [System.Serializable]
    9. public class Level {
    10.     public string sceneName;
    11.     public GameObject scene;
    12.     public int[] stars = new int[3];
    13.     public Vector3 cameraOffset;
    14.  
    15.     // Editor Variables
    16.     public bool editorToggle;
    17.     public bool editorStarToggle;
    18. }
     
  7. BrandonK

    BrandonK

    Joined:
    Sep 18, 2015
    Posts:
    41
    I feel so dumb now...

    You're a legend, thanks so much. It's working perfectly.
     
    karl_jones likes this.