Scriptable Objects in Unity

Ryan Sweigart
Nerd For Tech
Published in
3 min readSep 16, 2021

--

Objective: Use scriptable objects to fill-out a game object template.

We’ve been tasked with creating an interactive for visitors to a zoo. When the user clicks on an area of the map, we reveal a card with animals and facts that apply to that area.

Imagine if we had 50 exhibits. Creating a page for each one would take a lot work. Luckily, we can create one template and use scriptable objects to populate it!

Our scriptable object definition is really simple: Three strings to hold the text info and a sprite to hold a picture. All of our scriptable objects will contain these fields. The very first line, starting with [CreateAssetMenu… will allow us to use Unity’s editor to create scriptable objects using this template.

There’s our Card item in the Asset Menu!

We’ll create a new Card and name it BearDen. Then we simply fill-in the fields.

Here’s our card panel template. The Title Text, Info Text, Animals Text, and Animal Image objects will be filled-in by our scriptable object.

Here is our exhibit button script. When the button is pressed, the OnPress method will be called, passing our scriptable object to the UIManager’s DisplayCard method.

Here is our Bear Den button. We can see the ExhibitButton.OnPress method is connected to it.

Here is the UIManager’s DisplayCard method. It turns off the map panel, enables the card panel, and calls the card panel’s PopulateCard method, passing-in our scriptable object.

The final piece of our solution is the card panel’s PopulateCard method. It takes-in the scriptable object, and assigns each of the fields to its child objects.

And here is our finished Bear Den card!

--

--