I’m trying to fill a List in a monoBehaviour script with a serialized custom type trough a EditorGUI window, but the List always turns empty on play.
although i also set the same instance that gets created with the Editor window to an other mono script but as a variable and there it stays?
the class is properly serialized and all works fine except for that List.
this is the script with the List, its literately not more than the List
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ItemManager : MonoBehaviour {
public List<Item> itemList = new List<Item>();
}
this it the Item class
using UnityEngine;
[System.Serializable]
public class Item : ScriptableObject
{
[SerializeField]
private int _id;
[SerializeField]
private GameObject _thisItem;
[SerializeField]
private string _itemName;
[SerializeField]
private int _cost;
[SerializeField]
private string _description;
[SerializeField]
private Vector3 _itemPosition;
[SerializeField]
private Texture2D _icon;
[SerializeField]
private int _amount;
public Item()
{
_itemName = string.Empty;
_cost = 0;
_description = string.Empty;
_itemPosition = Vector3.zero;
}
public int ID
{
get{return _id;}
set{_id = value;}
}
public GameObject ThisItem
{
get{return _thisItem;}
set{_thisItem = value;}
}
public string ItemName
{
get { return _itemName; }
set { _itemName = value; }
}
public int Cost
{
get { return _cost; }
set { _cost = value; }
}
public string Description
{
get { return _description; }
set { _description = value; }
}
public Vector3 ItemPosition
{
get{return _itemPosition;}
set{_itemPosition = value;}
}
public Texture2D Icon
{
get{return _icon;}
set{_icon = value;}
}
public int Amount
{
get{return _amount;}
set{_amount = value;}
}
}
and this is how i add it to the list and how i add it to the item variable in the other script (which stays on play):
switch(typeToCreate)
{
case ItemType.element:
symbol = EditorGUILayout.TextField("Atomic Symbol : ", symbol);
if(GUILayout.Button("Create Item", GUILayout.Width(150)))
{
Element element=(Element)ScriptableObject.CreateInstance(typeof(Element)); //element derives from item...
element.Symbol=symbol;
element.ItemName=itemName;
element.Description=description;
element.ThisItem=itemObj;
element.ElectronObj=element.ThisItem.transform.FindChild("electron").gameObject;
element.Icon=Resources.Load("Items/KeyItems/element") as Texture2D;
new PeriodicTable(element);
itemManager.itemList.Add(element);
index = itemManager.itemList.IndexOf(element);
AssetDatabase.CreateAsset(element, "Assets/Items/"+index+"_"+element.ItemName+"_element.prefab");
Create(index);
element.PlaceElectrons(clone);
...
void Create(int index)
{
clone = Instantiate(itemObj, Vector3.zero, itemObj.transform.rotation)as GameObject;
clone.tag = "Collectable";
CollectItem collectItem=clone.GetComponent<CollectItem>();
collectItem.item=itemManager.itemList[index];
clone.name=itemObj.name+"_"+collectItem.item.ItemName;
}
}
if you read all of this, thanks for the effort!