Nerd For Tech
Published in

Nerd For Tech

Scriptable Objects in Unity

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!

--

--

--

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 https://www.nerdfortech.org/.

Recommended from Medium

How to convert any Office document file into plain text in Python

Week 2 — Halyes & Kittler

How to Parse and Validate a Full Name in C# .NET Framework

“Phase 5…Final Project…Job Search…” It’s finally here.

How to Compare Two Images for Similarity in Python

The Benefits of JWTs as OAuth2 Access Tokens

Decrypt a ZIP File in Swift

[Learning]#4 CSS Box Model, Selector, Absolute/Relative Position

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
Ryan Sweigart

Ryan Sweigart

An independent Unity Developer.

More from Medium

My journey becoming a Unity game developer: Make a game look beautiful-Door, Frame, and Wall…

5 Common Unity Game Development Mistakes

Prefabs in Level Design

Hidden Shortcuts to Make Level Design Easier