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

Custom inspector not saving changes

Discussion in 'Scripting' started by shotgunpanda, Mar 23, 2016.

  1. shotgunpanda

    shotgunpanda

    Joined:
    Nov 13, 2015
    Posts:
    5
    Hey guys,

    I have been banging my head against this one for a couple of days and am not making any progress. I prototyping a point and click game where objects in the game have actions associated with them when they are examined. I am trying to use scriptableobjects and custom inspectors for the first time so I apologize if this is a very noobish question.

    I have created a custom inspector that creates a new instance of a scriptableobject, saves it as an asset then adds this asset to a list of actions. This all seems to work ok, until I try and play the scene, where the list reverts to being empty. I think I must be misunderstanding how these are supposed to work on a fundamental level, but I don't seem to be able to get this to work correctly.

    Here is my custom inspector, for a Prop:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [CustomEditor(typeof(Prop))]
    5. public class CustomPropInspector : Editor {
    6.  
    7.     string[] options = new[] { "Speech", "Lock Door", "Unlock Door" };
    8.     int option_index = 0;
    9.  
    10.     Prop t;
    11.     SerializedObject GetTarget;
    12.  
    13.     void OnEnable() {
    14.         t = (Prop)target;
    15.         GetTarget = new SerializedObject(t);
    16.     }
    17.  
    18.     public override void OnInspectorGUI()
    19.     {
    20.         GetTarget.Update();
    21.         DrawDefaultInspector();
    22.  
    23.         EditorGUILayout.BeginVertical();
    24.         EditorGUILayout.LabelField("Create New 'On Examine' actions");
    25.         option_index = EditorGUILayout.Popup(option_index, options);
    26.  
    27.         string[] scene_path = EditorApplication.currentScene.Split(char.Parse("/"));
    28.         string scene_name = scene_path[scene_path.Length - 1].Split(char.Parse("."))[0];
    29.  
    30.         string path = "Assets/Game/Actions/" + scene_name + "/";
    31.  
    32.         if (GUILayout.Button("Create")) {
    33.  
    34.             switch (option_index) {
    35.                 case 0:
    36.                     string newPath = AssetDatabase.GenerateUniqueAssetPath(path + "Examine/" + t.data.id + ".asset");
    37.                     Speach obj = ScriptableObject.CreateInstance<Speach>();
    38.                     AssetDatabase.CreateAsset(obj, newPath);
    39.                     Speach newAsset = AssetDatabase.LoadAssetAtPath(newPath, typeof(ScriptableObject)) as Speach;
    40.                     t.actions_on_examine.Add(newAsset);
    41.                     GetTarget.ApplyModifiedProperties();
    42.                     break;
    43.             }
    44.         }
    45.  
    46.         EditorGUILayout.EndVertical();
    47.  
    48.     }
    49.    
    50. }
    BaseAction
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4.  
    5. [Serializable]
    6. public class BaseAction : ScriptableObject {
    7.  
    8.     public string action_name;
    9.     public string action_id;
    10.  
    11. ....
    Speach
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4.  
    5. [Serializable]
    6. public class Speach : BaseAction {
    7.  
    8.     public string text;
    9.     public bool active_character = true;
    10.     public string specific_character;
    11.  
    12. ...
    inspector.png

    Pressing the button creates the item, saves it to an asset and attaches it to the list correctly
    inspector2.png

    but when running the game, or saving and changing scenes the list returns to this:
    inspector2.png

    Any advice would be greatly appreciated! (Again, sorry for being a noob!)
     

    Attached Files:

  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,534
    Nate6 and tommyvisic like this.
  3. shotgunpanda

    shotgunpanda

    Joined:
    Nov 13, 2015
    Posts:
    5
    Hi - Thanks for the help!

    Adding
    Code (CSharp):
    1. EditorUtility.SetDirty(target);
    2. EditorApplication.MarkSceneDirty();
    in the custom inspector after adding to the list seems to have sorted it out :) My only worry is that the docs seem to indicate that EditorUtility.SetDirty is due to be removed from future versions of unity - so I will have to consult the docs further as to how I can update my code to future proof it.

    Thanks again!
     
  4. Georgie_Frankenstein

    Georgie_Frankenstein

    Joined:
    Nov 5, 2017
    Posts:
    1