Search Unity

Customer Editor scripting calling new ObjectName() not being saved

Discussion in 'Scripting' started by Hookkshot, Jul 23, 2014.

  1. Hookkshot

    Hookkshot

    Joined:
    Jan 11, 2013
    Posts:
    27
    I am having a problem where my classes that i try and put in the editor are not being saved. The reason I want to do this is so I can have a base class that can be overridden and then I can add multiple Behaviours to one item.

    Code (CSharp):
    1. public class Item : ScriptableObject {
    2.  
    3.     public string Name;
    4.     public float Cost;
    5.  
    6.     public List<ItemBehaviour> Behaviours = new List<ItemBehaviour>();
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.            
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.    
    16.     }
    17.  
    18.     [MenuItem("Assets/Create/Item")]
    19.     public static void CreateItem()
    20.     {
    21.         Item asset = ScriptableObject.CreateInstance<Item> ();
    22.        
    23.         AssetDatabase.CreateAsset (asset, "Assets/Prefabs/Items/NewItem.asset");
    24.         AssetDatabase.SaveAssets ();
    25.        
    26.         EditorUtility.FocusProjectWindow ();
    27.        
    28.         Selection.activeObject = asset;
    29.     }
    30. }
    Code (CSharp):
    1. [System.Serializable]
    2. public class ItemBehaviour : UnityEngine.Object {
    3.  
    4.     public string F;
    5. }
    Code (CSharp):
    1. [CustomEditor(typeof(Item))]
    2. public class ItemEditor : Editor {
    3.    
    4.     public override void OnInspectorGUI ()
    5.     {
    6.         Item script = target as Item;
    7.  
    8.         script.Name = GUILayout.TextField (script.Name);
    9.         script.Cost = EditorGUILayout.FloatField (script.Cost);
    10.  
    11.         EditorGUILayout.Separator ();
    12.  
    13.         EditorGUILayout.LabelField (script.Behaviours.Count + " Behaviours");
    14.  
    15.         for(int i = 0; i < script.Behaviours.Count; i++)
    16.         {
    17.             if(script.Behaviours[i] != null)
    18.             {
    19.                 EditorGUILayout.LabelField("Behaviour");
    20.                 script.Behaviours[i].F = EditorGUILayout.TextField(script.Behaviours[i].F);
    21.             }
    22.             else
    23.                 EditorGUILayout.LabelField("null Behaviour");
    24.         }
    25.  
    26.         GUILayout.BeginHorizontal ();
    27.         if(GUILayout.Button("Add Behaviour", GUILayout.ExpandWidth(true)))
    28.         {
    29.             script.Behaviours.Add(new ItemBehaviour());
    30.         }
    31.  
    32.         if(GUILayout.Button("Add Override", GUILayout.ExpandWidth(true)))
    33.         {
    34.         }
    35.  
    36.         GUILayout.EndHorizontal();
    37.  
    38.         EditorUtility.SetDirty (true);
    39.     }
    40. }
    When doing this I press Add Behaviour and the list size increases but is just filled with null entries.

    I would love people input on this. Its like the editor is just disregarding the reference I make to the new object.

    Sometmes after pressing the button this appears in the Debugger
    "Cleaning up leaked objects in scene since no game object, component or manager is referencing them
    Item has been leaked 1 times."