Using Scriptable Objects in Unity

Jon Jenkins
3 min readNov 10, 2021

--

Scriptable objects in Unity are a way to store data without having to use script instances. In other words, they are a way to store and pass data without having to have the data confined or dependent on a particular mono-behavior script. They exist in the editor and cannot be attached to game objects.

Here I will briefly discuss how to set up a scriptable object. For a good discussion of how scriptable objects can be used to improve your projects, look here:

Architect your code for efficient changes and debugging with ScriptableObjects | Unity

The First thing to do when creating a scriptable object is to declare a class which inherits from Scriptable Object. Also create an asset menu attribute for this class. This will allow you to create scriptable objects of this type from the asset creation menu. (Right clicking in the project window). The menu Name is what shows up in the creation menu.

Inside the class you can declare any variables that you might need. For example:

Now that we have a scriptable object set up, we can create one in the editor like so:

Now you can set the properties on this object, I’ve renamed it to Giraffe:

Above you can set the title, description, exhibit, and animal image properties. This scriptable object can be accessed by any script that has a property of it’s type. (Here, Card Model).

Above, the Card View class has a public array of type Card Model (our scriptable object). Now in the inspector we can simply drag over as many Card Model scriptable objects as we desire. Below, I have created six such objects and added them to the array in the inspector:

For the above project, I am using the scriptable objects to populate the data in the card view below:

Each scriptable object holds the title, description, exhibit information, and animal photo. Because I’m using scriptable objects, I only need to build one container view and then use the scriptable objects to populate the view based on what icon the user clicks on. This makes life much easier with minimal effort. The code I’m using to populate the data is seen below, where cards is the array of scriptable object and the (int card) is the index of the animal I want to show.

That’s basically it. This is a very basic way to use scriptable objects to make life a little easier when dealing with data.

--

--