Optimization: Scriptable Objects

Dan Schatzeder
3 min readFeb 21, 2021

--

Reduce memory usage, organize, and make your project easily designable with Scriptable Objects!

As grows the size of any system, so grows the complexity, and so the accompanying inconvenience of additions or adjustments made to it. You will find that the deeper you delve, the higher the likelihood that one small tweak can corrupt your entire structure. Complexity cannot be avoided, but it can and should be handcuffed to its sober sponsor, Modularity.

Modularity is “the degree to which a system is made up of relatively independent but interacting elements, with each module typically carrying an isolated set of functionality. It is a mechanism for complexity redistribution.”¹

Simply put, a good complex system should be made up of small, adjustable pieces that can interact with while remaining independent from one another. One such tool in the quest for modularization is the Scriptable Object.

To sell it quickly, the Scriptable Object is an intelligent data container in Unity. It’s a lovely standard by which to organize any information database. It can be thought of as an interactive spreadsheet with which to organize, adjust, and categorize different aspects of a database. At its highest form it’s an incredibly lightweight and diverse design tool with which to simplify and communicate between different parts of your Unity project. It’s a major opportunity for restructuring your projects in a macro sense. But onto its simple usefulness…

Hearthstone, designed using Unity; each Card is a Scriptable Object! Think of all the variables here: Cost, Health, Attack, CardName, Description, Ability, Type, Artwork. All of them individually adjustable, stored neatly in the Inspector, and weighing in at just a few Kilobytes of data!

The Scriptable Object’s fastest friend is the database. Are you building a video game that has a list of items, spells, enemy waves, or tables of information to store? With the Scriptable Object, once you define what’s in each of them, you can create and adjust them in the Inspector without so much as looking at a line of code. Now any game designer can jump into your user friendly interface and make adjustments if they need to.

It takes very few steps to get started.

  • Create script and change inheritance from Monobehaviour to ScriptableObject
  • Add CreateAssetMenu attribute to the class, define your own Menu entry and use “/” if need to denote a hierarchy for separate entries
Remember [SerializeField]? It’s not the only useful Attribute!
  • Define any variables and you’re done.
Create and organize your own menu entries! Use / in your menuName to create new sub-menus.

Start basic, and once you continue to learn with Unity, the potential of these will creep up on you. Define the probability table for loot from types of treasure chests throughout your game. Define the stat modifiers for each Level your character gains. Detail the behavior of each enemy wave in a Tower Defense, or the different variables that will accompany varying levels of difficulty your game is played at.

A lovely example from HealPlzGame.com of a complex Scriptable Object, encapsulating each aspect of an individual Spell! All in one designer friendly space.²

Like a new pair of glasses, they may strain your mind at first, but give them time and you’ll scoff at ever having done without them. And be careful not to get sucked in too deep into the rabbit hole like I did this weekend.

--

--