Script Communication in Unity

Using Scriptable Objects

James Lafritz
Dev Genius

--

Scriptable Objects make it easy to pass data between Game Objects, this is especially useful for global variables that are used in your game and helps to keep your Game Systems Modular. For example you have a spawning system that only spawns objects if the player is alive. Your UI System displays the Score, the Players Lives and other data. You can’t use the spawning system or the UI system without the player.

This is inspired by 2 Unite talks Unite Austin 2017 — Game Architecture with Scriptable Objects and Unite 2016 — Overthrowing the MonoBehaviour Tyranny in a Glorious Scriptable Object Revolution. To read more about Scriptable Objects see the Unity Manual and the Scripting Reference.

I used code from https://github.com/roboryantron/Unite2017 as the base for my variables and Game Events. I created variable and variable refrences for the common variable types string, int, float, bool, Vector2, Vector3. I started with an Abstract Variable Base Generic, A Variable Reference Generic that takes a Variable Base, and a Static Variable Reference Property Drawer (To make it look like it was built into Unity).

How the int type is set up the other types are done the same way.

Notice for the only code needed is for the Add Method

Notice that there is no code here except for the class definition.

For the property drawer this was what I had to for to get the property drawers to work for each individual type. Notice it just calls the Variable Reference Property Drawer On Gui Method.

Initial Setup

In order to display the lives the player has we need to find the Player Game Object and Get it’s Damageable Component to Get the number of lives it has.

Using Scriptable Object Init Variable

The UI system doesn’t care where the value that it is displaying comes from as long as it has the information that it needs. Lets make things easier on us and the performance of our game and use a Scriptable Object Int Variable.

Update the Lives Updater Script

I am going to use the Int Reference type here just incase the game does not have this variable in it and we are just using the UI, we can adjust this value in the inspector to test if our UI is working.

Much easier to get the information that we are looking for.

Update the Damageable Script

We are using the Int Reference type here so we don’t have to have a scriptable object variable for every single game object in the game. The Player Lives is the only one that we need this for. We will allow the game designer to decide which variables they need.

Using a Scriptable Object Variable

Creating the variable.

Using the variable in the game.

The purpose of the Property Drawer is to draw the nice interface in the Inspector window. You knew exactly what the Component is using for it’s data. Without the property drawer script both the constant value and the variable value would be shown in the inspector and it might be confusing.

The Game Using the Scriptable Object Int Variable

Using a Vector 3 Scriptable Object Variable for the Movement Direction

Update the Player and Movable code to use the moment direction.

Player now using Vector3 Variable

Notice that I am using the variable and not the variable reference. I just showing the difference with using the 2 different types in code.

With a Variable type you have to use SetValue or Add Method to change it.

Movable now using Vector3 Reference

Create the Player Movement Direction Variable and set the move direction to use this variable. Since we change the type we have to go to every Game Object in the game and make sure the move direction has the right value.

Game works the same.

Notice that as I move around and take damage you can see the values update in real time. The values are changed in Play Mode, After Leaving Play Mode they do not reset like other Variables that you change in Play Mode.

--

--

Excited about changing my hobby into a new carer with GameDevHQ course.