Hi there,
I’m having trouble exposing derived classes in the inspector.
I have a BaseClass
using UnityEngine;
using System.Collections;
//expose in inspector
[System.Serializable]
//no inherit
public class BaseClass {
//fields
public string m_name;
public string m_desc;
//constructor
public BaseClass () {
m_name = "empty name";
m_desc = "empty desc";
}
//properties
public string Name {
get{return m_name;}
set{m_name = value;}
}
public string Desc {
get{return m_desc;}
set{m_desc = value;}
}
}
And a DerivedClass
using UnityEngine;
using System.Collections;
//expose in inspector
[System.Serializable]
//inhertis from BaseClass
public class DerivedClass : BaseClass {
//fields
public int m_viscosity;
public int m_temp;
//constructor
public DerivedClass () {
m_viscosity = -1;
m_temp = -1;
}
//properties
public int Viscosity {
get{return m_viscosity;}
set{m_viscosity = value;}
}
public int Temp {
get{return m_temp;}
set{m_temp = value;}
}
}
I add three instances of DerivedClass to a list called MyList
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MyList : MonoBehaviour {
// My List
public List<BaseClass> myList = new List<BaseClass>();
// Base Class
public BaseClass newBaseClass = new BaseClass();
// Derived Class
public DerivedClass newDerivedClass = new DerivedClass();
// Use this for initialization
void Start () {
PopulateList();
}
// Add three empty derived classes to the list
public void PopulateList(){
for(int i = 0; i < 3; i++){
myList.Add (new DerivedClass());
}
}
}
I attached MyList to a game object.
When I run the game and check the contents of MyList in the inspector, it only shows a BaseClass instance for each list element, that is I see a ‘Name’ and ‘Desc’ in each list entry. I know that the DerivedClass is there too, because I can access its fields via code, it just doesn’t show in the inspector.
Does anyone have any idea how I’d go about showing DerivedClass so I can see ‘Name’, ‘Desc’, ‘Viscosity’ and ‘Temp’ in each list element in the inspector?
Thanks,
Pli
P.S. just to note, this is for learning purposes so that I can see what’s going on in the list. I would make the m_ fields private in reality which is why they have the prefix.
P.P.S is it normal to keep finding a mountain twice the size of the one you just climbed when learning a language for the first time?