Optimization: Scriptable Objects

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.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Robot Operating System: How to Create a Robot Simulation Model (Part 2)

Dev Diaries #09–25 March 2022

Constructing a Basic Chatbot about Physics

The best GUI libraries for Rust in 2022

A painless way to manage secrets in Google Kubernetes Engine

Network Capacity Management Using Proactive Network Maintenance

What coding courses DON’T teach — and why it really matters

Creating a sparse signed Win32 app package

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
Dan Schatzeder

Dan Schatzeder

More from Medium

Performance Tips for Unity — Render Optimization #2

Variables! — The Building Blocks of Programming

Day 23 of Game Dev: How to Load Scenes in Unity through Code!

To pull the trigger or not pull the trigger, that is the collision.