Nerd For Tech
Published in

Nerd For Tech

Using Scriptable Objects in Unity

Don’t Get Frustrated, Get Templated

Time to monkey around.

Today’s project is a very simple app with a few neat features I haven’t written about before. The first of these is the Scriptable Object.

In essence, Scriptable Objects are containers for data. You can design a general data template, create assets out of that template, and refer to those assets in code to populate data in runtime objects. It’s an excellent way to avoid duplicating tedious work.

The app we’re developing is an interactive map of a zoo. Zoo patrons can use the map on their mobile device to look at detailed information for each exhibit in the zoo, as well as locate those exhibits and park amenities. Here’s the map:

Each exhibit label is a button. The idea is, when we click on an exhibit we get a panel that looks like so:

Let’s take a closer look at how scriptable objects can help us here.

The Scriptable Object

Now, we could create individual cards for each exhibit (there are only 6, after all), but that’s still a lot of repeated work. Imagine if we had 60 exhibits! That’s where the scriptable object comes in. We’ll create variables for each part of the card that needs to be replaced, and then we can load the data from our scriptable objects into our exhibit card in the app.

Time to see how that shakes out in C#:

So, we’ve done two things to make this work: we’re inheriting the UnityEngine.ScriptableObject class, and we’re adding some editor script that will place an entry on the Create Asset Menu that will allow us to create scriptable objects based on our CardModel.

Next we’ll create the objects and fill them with data. By right-clicking in the Project view and selecting Create > Cards, we’ll create a new Scriptable Object asset based on our CardModel script.

We can fill in the proper data here. But then how does it get populated into our Card Panel?

For that, we’ll create an Action.

Actions and Events

Right now our data is in Scriptable Objects waiting for us to use. We need a script that can be responsible for changing the card view — we’ll call that CardView — but how does it know where to find the data or what data to use?

For that, we’ll create another script that will interact with all the buttons on the map panel. We’ll call that MapPanel.

The MapPanel script looks like this:

The first thing we do is declare an Action. Actions are essentially Delegates and Events wrapped into one line of code. We’re delegating to any method that accepts a CardModel parameter, and we’re watching out for the event OnSelectExhibit.

Next we have the SelectExhibit(CardModel card) method. Here we’re turning on the cardPanel object and notifying anyone listening that the OnSelectExhibit event has occurred, passing in the card Scriptable Object for listeners to use.

SelectExhibit gets called by the UI button’s OnClick action, which also passes in the relevant Scriptable Object to the MapPanel class to send to listeners.

We’ll talk about the Analytics Event tomorrow.

So, the button tells the MapPanel what button got pressed. The MapPanel tells any script listening what button got pressed. And now, the CardView class can go to work:

The crucial parts here are the OnEnable() method and the FillCardData(CardModel card) method.

In OnEnable, we subscribe to the OnSelectExhibit event with our matching method: FillCardData(CardModel card).

In FillCardData, we assign all the fields in this script to the corresponding fields in the card Scriptable Object passed along to it from the button click.

Now we have just one Exhibit Card panel which can be instantly populated with data from any of the Scriptable Objects based off our CardModel class. Shall we watch the magic happen?

We shall:

Hey, that works great!

That’s what I have for today. Tomorrow we’ll go over using Unity Analytics to track how often users are requesting information on each exhibit.




NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit

Recommended from Medium

TWTW Week 3,ENT101 — Day and night thinking about BOT

Unique Types of Body Settings in Virtual Cameras

How to simplify complicated software?

Use Case of AWS SQS

The Dining Philosophers — Deadlock Problem and Solution

Using skittles to shape software engineers

Pop first element of a queue in Python — list.pop(0) vs collections.deque.popleft()

Real world AWS — Using custom CloudWatch metrics to monitor disk space

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
Micha Davis

Micha Davis

Unity Developer / Game Developer / Artist / Problem Solver

More from Medium

Journal 142 — Unity Project Development, 2.5D Shooter

Switch Statements to the Rescue

Flip Anything Moving— Version 2

A Series On Creating Immersive Audio in Unity with Custom Wwise Scripts