Scriptable Objects in Unity

July 31, 2019


Scriptable objects in unity can be imagined as if a data model has been saved as a prefab. They are very useful and handy when it comes to making multiple presets for a gameObject with a different set of properties and features. With scriptable objects, this can be done very easily by just replacing the existing scriptable object with a new one.

Creating a scriptable object

To create a scriptable object, we start with creating a C# script that derives from ScriptableObject class instead of the MonoBehavious class.
Simply, create a C# script named MyScriptableObject.cs and change the inherited MonoBehaviour to ScriptableObject.
In order to create a prefab for a scriptable object, we need to add the following attribute over the MyScriptableObject class name to be able to create a scriptable object from the context menu.
The attribute CreateAssetMenu takes the following parameters:
menuName — it is the menu name that will show up under the Createcontext menu under the Project view

Notice here, we didn’t attach the script to any gameObject in the Project hierarchy
fileName — it is the default name of the file that will be created for the scriptable objects

At this point, the MyScriptableObject.cs file will look like:

Modifying the Scriptable Object

Now we will be modifying the scriptable object to alter properties of a gameObject by just replacing one scriptable object with another.
For simplicity, we will be changing the position & the scale of the gameObject with the scriptable objects.
To do so, start by adding the following fields in the MyScriptableObject.csfile.

Creating the Scriptable Objects

Now create a scriptable object by clicking the Create -> My Scriptable Object in the Project view. This will create a file similar to the gif above. On selecting the scriptable object created, the inspector shows the following view:

These are the fields that we will be using to set the gameObject’s position and scale.
Modify the Position & Scale in the inspector to some different values,
say, Position is (10,10,10)
and, Scale is (5,5,5)

Using the Scriptable Object

Now that we have created a scriptable object in the Project view, now we will use this scriptable object to update the properties of a gameObject.
To use a scriptable object, start by creating a C# script with any name(say ModifyGameobject.cs).
Now add a field to store the reference to that scriptable object.
In the Scene view, create a 3D Cube & attach the ModifyGameobject.csscript to it.
Now drag and drop the scriptable object we created earlier to the GameobjectData field of the ModifyGameobject.cs

Now modify the Update() method of ModifyGameobject.cs to the method below:
Now run the game. You will see the cube moved to the position (10,10,10)with a scale of (5,5,5) as described in the Scriptable Object we created and assigned to the ModifyGameobject.cs.

Benefits of Scriptable Object

Scriptable objects make the game more dynamic with much ease. With the use of scriptable objects, one can save a lot of different presets for gameObject(s) that too in very little space. The scriptable object created is no more than a few kilobytes in size that makes it size efficient.

Dynamic Nature of Scriptable Objects in Action

In the game scene, we just created, create another scriptable object with some other values of Position and Scale but don’t replace the original assignment of GameobjectData in the Inspector view. We will do it dynamically.
Now run the game. Now see we have the cube at (10,10,10) with a scale of (5,5,5). Now with the game running, replace the GameobjectData with the new scriptable object we just created and see the change in the cube’s Position and Scale.

You Might Also Like

0 comments

Follow by Email