- Home /
Custom abstract class (and inheritance) not allowing public array despite having System.Serializable?
Hey all,
I've searched similar posts, but must be missing something as I'm not getting the desired result.
Basically, playing around with basic AI to get understanding down. Have a base class as follows:
[System.Serializable]
public abstract class ActionBaseClass {
public abstract void SetupAction(StateHandler stateHandler);
public abstract bool AbleToPerformCheck();
public abstract bool PerformAction();
}
I then have a 'concrete' class that inherits from this:
[System.Serializable]
public class Action_MoveTo : ActionBaseClass {
StateHandler ourHandler;
...
And finally a controller that SHOULD hold an array of the available actions to it - as follows:
public class StateHandler : MonoBehaviour {
[SerializeField] // I have tried it with this line missing as well.
public ActionBaseClass currentAction;
[SerializeField] // I have tried it with this line missing as well.
public ActionBaseClass[] actions;
...
However when the scripts are compiled I am NOT seeing a public array or single variable in the inspector...
As stated I have tried it with both "[SerializeField]" lines missing - however I'm still getting the same results. Any help would be gratefully appreciated.
Many thanks!
Answer by Bunny83 · Jul 19, 2017 at 05:04 PM
Unity does not support inheritance when it comes to custom serializable classes. The serialization system does not serialize the actual instance but only the fields as "sub-fields". However it always uses the type of the variable and not the type of the instance that might be referenced. Since your variable type is "ActionBaseClass" it only serializes fields which are defined in that class.
If you need inheritance you have to use either MonoBehaviour or ScriptableObject derived classes.
For more information read the Script Serialization page carefully
Dear Bunny83,
$$anonymous$$any thanks for this answer. During my research I never came across that page - it was exactly what I needed. Again many thanks XD
That sucks. I have a ScriptableObject for my level data, which requires inheritance. I'd prefer to not clutter my files with various scriptable objects for every single orientation of a tile. I know I should use the scene view for tile stuff, but for a puzzle game, I don't really like working that way.
I'd prefer to not clutter my files with various scriptable objects for every single orientation of a tile
Why? It's generally recommended to place one class in one file and name the file accordingly. This provides a much better overview when browsing through the project files. It also helps when multiple people work on the same project since each script file is a seperate entity inside a C$$anonymous$$S and lowers the chance of conflicts.
$$anonymous$$ost Java applications even go a step further. Not only has every class its own file but the folder structure usualle maches the namespace hierarchy. While it sometimes seems like overcomplicating things it really helps organising your code in the long run and you will find the code you're looking for more quickly. If you have several subclasses of a certain type, just create a subfolder. There are many different ways how to organise a Unity project.
Finally note that while inheritance can be a useful tool in many cases, when it comes to serialization this is generally more an issue than a solution. $$anonymous$$aybe you should re-think your approach how you serialize your data. Note that the [ISerializationCallbackReceiver][1] interface can be used for various interesting workarounds. So you can transform your data manually into something Unity can serialize.
Though if you need help with your specific case you should post a seperate question. [1]: https://docs.unity3d.com/ScriptReference/ISerializationCallbackReceiver.html