Unity Scriptable Objects Tutorial Updated

September 24, 2020

Unity

Scriptable Objects


Title: Better Data with Scriptable Objects in Unity! (Tutorial)
By: Unity
Youtube – Tutorial
Description: Quick updated rundown by Unity themselves on using Scriptable Objects.


Title: Scriptable Object Unity Documentation
By: Unity
Unity – Documentation
Description: One of the suggested links to follow from the tutorial video that is just the official documentation on scriptable objects.


Title: Making cool stuff with ScriptableObjects
By: Matt Schell
Unity – Blog
Description: Another suggested link to follow from the tutorial video showing some more examples of using scriptable objects.


Overview

I wanted to make a quick reference to this new video as it was just released yesterday (Sep 23, 2020) and I have wanted to explore scriptable objects in Unity and getting recently updated information is always useful.

Scriptable Objects as Data Containers

Scriptable Objects can be useful tools as data containers as is shown in their example using them for cards in a card game. They explain they can be a better option here than a Monobehaviour because Monobehaviours must be attached to a gameobject, where as Scriptable Objects can be used similarly to regular objects in that they do not have this requirement. If data does not belong to an instance, but is more shared amongst game objects, it is better to store it in a scriptable object. This is because when Monobehaviours reference Scriptable Objects they store a reference that points to the scriptable object as opposed to making their own copy of the data.

Scriptable Objects as Enum States

Scriptable Objects can also be used to author extended Enum states. They explain this through an example where they have a Placeable Data scriptable object which has Enum options for their Attack Type and their Target Type. This appears to just mean they support Enum lists which then allow for an easy dropdown option in the Inspector (similar to how Enums generally work in my experience).

Runtime Data Editing

They expand on the benefits of using scriptable objects as extended Enum states is that they allow for real time data editing (while in play mode). Normally most edits made in the Unity Editor during play mode are not saved so you have to remember them and reapply them if they are changes you want. Scriptable objects however can have their edits during play mode saved on the spot so they will persist even after leaving play mode. This is because scriptable objects are not bound to a scene’s runtime, they exist on a project basis in the Assets folder.

UnityLearn – Beginner Programming – Creating a Character Stat System – Pt. 01 – Overview and Creating Scriptable Objects

January 8, 2020

Beginner Programming: Unity Game Dev Courses

Beginner Programming: Unity Game Dev Courses

Unity Learn Course – Beginner Programming

Project Overview

General Overview of Creating Character Stat System Tutorials

  • Defining and Configuring scriptable objects
  • Accessing data inside of scriptable objects
  • Setting up helpful debugging items
  • Scriptable objects inside of monobehaviours

Creating Scriptable Objects

Scriptable Objects Explained

Scriptable objects are very effective for creating designer tools.

Systems Map: image showing the separate pieces of the game system and how they all connect, in a web-like design

SCriptable Objects: data containers of which, we can have multiplie instances

  • Defined by programmer
  • Treated as assets in Unity
  • Easy for designers to work with
  • CANNOT be added to gameObjects as components

The basic template for turning a script to a scriptable object is adding a [CreateAssetMenu()] header to the top of the script and having the class inherit from SCriptableObject. The CreateAssetMenu header is what adds the scriptable object as an option in Unity’s own Asset Menu at the top of the screen. This also holds the information on how it is displayed there.

Again, it is important to note that scriptable objects CANNOT be added to gameObjects as components. This is worked around in the tutorials however by adding them as a field to a Monobehaviour that is located on a gameObject.

Debugging the Aggro Radius

This segment mostly dealt with importing the project if you had not already, and doing some folder organization for these tutorials. They also added a bool to control showing aggro radius of enemies.

Building Your Scriptable Object

The first scriptable object they create is called CharacterStats_SO.
The CreateAssetMenu tag used at the top of the script is directly used to store this information in an item in the drop down menus of Unity itself. This is why there is a fileName and a menuName.

MenuName: This will be the string seen in the drop down menus directly. You can also add a “/” to create a chain of drop downs where the item will be located.

The fields used for CharacterStats_SO:

  • bool setManually: this will determine if the stats were created manually, or created dynamically
  • bool saveDataOnClose: this will determine if data should be serialized, or written to disk, on close
  • The rest are just general player stats fields (health, damage, resistance, etc.)

SUMMARY

These tutorials were just the tip of getting introduced to scriptable objects in Unity. The rest of the tutorials in this set will delve deeper into the topic and how to incorporate them into other Monobehaviours.