[System.Serializable]
public class baseList
{
public int variableOne;
}
public class Test : MonoBehaviour
{
public List<baseList> testList;
}
and when we create a child of the Test class we realize we want to add more variables to the baseList class
[System.Serializable]
public class baseList2 : baseList
{
public int variableTwo;
}
public class Test2 : Test
{
public List<baseList2> testList;
}
The problem now is how do you override testList in new Test2 class which is a child of the Test class?
The list needs to be exposed to the editor and the new keyword does not work here. Anyone got any suggestions? Or do I just not understand this inheritance thing enough
First of all you can’t “override” a field of class. Never. Overriding only applies to virtual methods. Non virtual methods or fields can be “hidden” / reimplemented but keep in mind that you’ll end up with two versions of the field which are both there in the derived class.
Second, Unitys serialization system doesn’t support inheritance for custom classes, at all. So what you try to do here simply isn’t possible. Actually your List< baseList > can store references to both baseList and baseList2 instances. This would work at runtime just fine, however Unitys serialization system stores custom classes based on the field type which is “baseList” in your case. That means all instances of baseList2 you store inside your testList at edit time will become baseList instances once they have been serialized / deserialized.
The example you provided is again way to abstracted to give you any advice besides “what you try to do here simply doesn’t work”. You should describe more in detail what you actually want to do, what the base class is used for and why you actually need to derive the other class from the base class.
If it’s just for not having the same code twice you might want to look into using a generic base class. Of course different generic implementations aren’t comparible to each other. Again, with the information given you won’t get a more detailed answer than that.
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Example {
public string Number;
// Use this for initialization
void Start () {
}
public enum Type {
number
}
// Update is called once per frame
public Example (string num) {
Number = num;
}
}
and the script to turn it into an array
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class list : MonoBehaviour {
public List<Example> database = new List<Example>();
// Use this for initialization
void Start () {
}
}