Tips: ScriptableObject

I already talked about ScriptableObject in a previous article, there are many reasons to use ScriptableObjects. They can increase your workflow, reduce memory usage. You have to take into account that a ScriptableObject follows the Flyweight design pattern, and this lightens the memory usage.

To use the ScriptableObject we have to create a new script and modify its content. As the first thing, we have to remove the MonoBehaviour because it will inherit directly from ScriptableObject and it will be necessary to insert the keyword “CreateAssetMenu” with the various fields, before the class itself. So that it can be serialized in Unity menus.

Now you just need to define the parameters you need based on what you need to accomplish.
This tool can be used in several ways, for example:

  • with different types of players/enemies, we can define the necessary data, it can be the statistics that are common between the various enemies
  • you can also create a system of waves of enemies by creating a ScriptableObject that contains the list of enemies that should be in a particular level
  • as I have shown in the previous article you can use it, even in a broader way, for the realization of a card game, outlining the various fields and using an enum to create a list of special abilities that can then be interpreted by the game manager.
  • another of the most common uses is the management of game objects, we can create a ScriptableObject and then produce data for each type of object in the game

A fundamental thing to keep in mind is the concept of inheritance in C# that also applies to ScriptableObjects, taking as an example the realization of a database of objects of a game, we can distinguish the objects in more categories and define the parameters that can be common in the parent class, and then build scripts that will inherit from that class and assign them more specific names (eg consumables, weapons, armor, unique items, etc. …).

I hope this article has been useful to better understand the potential of ScriptableObjects.
Good work and see you next time.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store