Chapters

Hide chapters

Unity Apprentice

First Edition · Unity 2020.3.x LTS Release · C# · Unity

Before You Begin

Section 0: 4 chapters
Show chapters Hide chapters

8. Scriptable Objects
Written by Eric Van de Kerckhove

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the previous chapter, you learned the basics of Unity’s user interface system and used the most common UI elements to create a title screen and a window. This knowledge will come in handy when completing the game with a flexible dialogue system that you’ll also be able to use in your own games with ease.

This chapter is all about giving the NPCs a way to talk to the player’s avatar and letting the player choose how to respond. This lets you add some personality to the characters.

The dialogue system you’ll create will teach you the basics of how to use scriptable objects to save conversations as files. This same concept will allow you to create items, weapons and settings presets as files for your own games. Scriptable objects are immensely powerful once you know how to use them, and they’ll make your game development journey a lot easier.

Dialogue user interface

Before diving into the concept of the dialogue system, there’s a new window you need to meet: the Dialogue Window. This GameObject is a child of Canvas in the Hierarchy and is disabled by default to hide it. Open the Chapter 8 Starter project in Unity and then open the Dining Hall scene in RW / Scenes. Then, expand the Canvas and select Dialogue Window. In the Inspector, click the checkbox in front of its name to make the window visible in both the Scene and Game views.

To get a good look at the different parts, fully expand and enable all of Dialogue Window’s children in the Hierarchy.

Take a look at the bottom of the Game view to see the Dialogue Window. The dialogue window might seem quite a bit more complex than what you’ve seen up until now, but it’s actually just made up of some images and text.

Here’s an overview:

  1. Speaker Tag and Speaker Text: The tag here is a dark image element that automatically resizes itself to the size of Speaker Text. Speaker Text is a text element that shows the name of the speaker — this can be “Guard” or “Potato guy,” for example. You can test this out by changing the text value of Speaker Text.
  2. Line Text: This is a text element that shows what the speaker is saying or asking.
  3. Option Selection Image and Answers: The white triangle on the left is an image that acts like a selection cursor. It can be moved to highlight the selection option. The two text elements are the possible answers when a question is asked. All of these UI elements will only be visible when a question is asked.
  4. Continue Indicator Image: This is an arrow image that blinks on and off via a small script. It’s used to indicate that the interact button can be pressed to advance the conversation.

Now that you know the different parts of the dialogue window, disable Option Selection Image and Answers again, followed by the Dialogue Window GameObject itself so it won’t be in the way. From now on, the different parts will be shown or hidden with scripting.

Dialogue manager overview

This brings you to the Dialogue Manager — it’s a child of the GameObject called Managers in the Hierarchy.

Creating a dialogue system

A dialogue system in video games can assume many forms, but in essence it allows you to speak with characters and make a choice now and then. Think of old school RPGs like EarthBound, Secret of Mana or Breath of Fire — they all allow you to walk around and talk to the people (and creatures) around you.

Unqapuxv Cgas urs ulbiko BeocinieVhevnit Zeku: Teqhoaduw Pexg: Wext ci Rebvovqacieb Pofkepmijeic Djeiqep Cihc Fauvasui Luwa Jayj Xu! Heihufoi Keri For Vocdu. Gaunedea Rilo XeaqojiiXewaxov Inliqpa hidzekpuxeiz Niorimee Tudyeq

Lines of dialogue

The first order of business is to create the DialogueLine class, which holds the data for a single line of dialogue. Create a new C# script named DialogueLine in RW / Scripts / Dialogue and open it in your code editor. Unity’s default script template will make this yet another class that derives from MonoBehaviour to become a component. DialogueLine shouldn’t be a component, though, but a simple class that holds some variables instead.

Conversations

A basic conversation is a simple list of dialogue lines with one or more speakers that will play one line after the other like a movie script.

Writing scriptable objects

There’s a far better alternative for storing data in a central location, and it’s built right into Unity: scriptable objects! You can use a scriptable object as a data container and store it as an asset, like an image or a sound effect. You can then reference the scriptable object in a component and read its contents. As a bonus, you can edit its properties right in the Inspector like components and the asset files are easily edited in a text editor. Yes, scriptable objects are truly marvelous, and you should use them when you can.

Creating scriptable objects

Create a new folder in the RW folder named Conversations, and right-click on the new folder. Next, select Create ▸ Dialogue ▸ Conversation to create a new conversation, and name it Weird Taste.

Dialogue starter component

To make the NPCs interactable, you’ll use the interaction system. You learned about this in the previous chapter, but here’s a quick refresher:

Improving the dialogue manager

The Dialogue Manager serves three purposes:

Starting a conversation

The first order of business is to add these variables below the // Conversation comment, above the Awake method:

Showing more lines of dialogue and ending a conversation

First, the avatar must be able to interact with the dialogue starter again once it’s done talking. To achieve that, open the DialogueStarter script again from RW / Scripts / Dialogue and add the following method below Interact:

Handling questions

Now that you can let the characters talk to the player avatar, it might be interesting if they can ask questions as well. This makes the conversations feel more dynamic and interesting. Every line of dialogue will have the option of becoming a question, which will prompt the player for two possible answers. Each of these answers will be linked to a different conversation asset that will be loaded by the Dialogue Manager.

Safnintemeiq Om, teof heemmorm. Holzuldaqoeb Dam’l eog plo saej. U qpijy iw’j jahmg. Nurseydimuat Tosgo! Pu rie botn asguwu? Qo. Qor!

Dialogue question class

First, create a new C# script in the RW / Scripts / Dialogue folder and name it DialogueQuestion. Open the new file in a code editor and remove both the Start and Update methods. This will be another serializable class like DialogueLine, so replace the class declaration line with this:

Setting up the editor for questions

The boss of this place is standing before the huge wooden door at the back. She’s going to ask the player avatar whether he’s ready to enter the arena. The player will then have the choice of answering yes or no, which will trigger another conversation. In other words, you’ll need to create three conversations for the boss.

Preparing the dialogue manager for questions

The dialogue manager can’t differentiate between statements and questions at the moment, so it’s up to you to make it smarter. This involves some more scripting, of course, so open the DialogueManager script from RW / Scripts / Dialogue in your code editor once more.

Key points

  • A dialogue system allows you to speak with (non-playable) characters and make choices.
  • Variables from regular classes can be edited in the Unity editor by marking them serializable using the [Serializable] attribute.
  • Instances of serializable classes can be saved to and loaded from a disk or memory.
  • Unity comes with a lot of built-in property attributes to help display your components the way you want to in the Inspector. A good example is [TextArea], which creates a multiline text field for a string variable.
  • You can use a scriptable object as a data container and store it as an asset.
  • Use the [CreateAssetMenu] attribute on scriptable object scripts to add them to the Create menu in the editor.
  • Use LayerMask.NameToLayer("layer name") to get the ID of a layer.
  • Test your scripts along the way by using print() to make sure what you have up until that point works.
  • A coroutine is a method that can spread its execution over multiple frames.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now