Hey sorry to bother you all, I’ve been working on setting up my first rpg game in Unity. I wanted to test out the combat system and have been using the inspector to really quickly throw together some basic stats and moves to test things out, in the final version everything will be handled through data loaded in through code, but just for now to quickly mess with values I was wondering if there was an easier way to handle something.
I have a class that handles the different characters and they abilities they can learn, as well as a class of what an ability looks like being handled in a single gameobjects script. At the moment when I open the learnable abilities list, each element holding a level it is learnt at and the ability within. I was wondering if there was a way to call an ability from the all abilities list via the inspector instead of having to type in the same ability as a separate instance in the learn able abilities
[128202-serialize-1.png|128202]
No, this is not possible with the serialization system Unity uses when you use just custom serializable classes. Such classes (or structs) actually behave like structs and can not be referenced in the inspector. Unity does support serializable references, but those are only supported for classes derived from UnityEngine.Object. There are only two runtime base classes which you can derive your own classes from. Those are: MonoBehaviour or ScriptableObject.
If your ability classes do not directly belong to a gameobject you probably want to derive it from ScriptableObject. Note that in order to reference such a class instance you actually have to store the instance of the class as asset in your project. UnityEngine.Object derived classes are never shown “inline” like custom serializable classes / structs. Instead you will have an object field where you can drag and drop an instance in order to reference that instance.
If you are relatively new to Unity i highly recommend to read the script serialization page very carefully, especially the section “When might the serializer behave unexpectedly”.
Thanks to the “CreateAssetMenu” attribute it is now very easy to work with scriptable objects. You just create an instance of your ability class like you would create a Material asset.
Note that if you reference an asset by one of your script and perform some manipulations on the data fields of that asset instance during playmode, those changes would persist when testing inside the editor. If you somehow need an independent instance at runtime, you can simply use Instantiate to create an instance / copy of your scriptable object.