Moongate Games UK

"Changing worlds, one line of code at a time"

Tutorial: An Introduction to Scriptable Objects

Subject: Scriptable Objects

Language: C#

Scriptable Objects when I first heard about them seemed like little scripts of magic (similar to that of CoRoutines) but now that I've started using them I've found out how useful they are.

This tutorial will cover the basics of creating and using scriptable objects in the way that I go about in A Way Home. Before all of that though in addition to this tutorial, this video is worth a watch if you haven't watched it already:


Creating Actions:

 To start with when for creating the actions we'll need the base class which will just be called Action.cs this will be what the specific action classes Inherit from (side note: If you don't know about inheritance and abstraction etc. I'd recommend searching in google for it as it's good to understand what's going on.).

The Action class will look like the following:

using UnityEngine;

public abstract class Action : ScriptableObject
    public abstract void DoIt();
    public abstract void DoIt(GameObject go);

Next will be a more specific action, for this, it won't be anything particularly spectacular just showing a Debug message etc. 

For this, you'll need to create a new class called FirstAction.cs and then populate it with the following:

using UnityEngine;

[CreateAssetMenu(menuName="Actions/First Action")]
public class FirstAction : Action {

    public int Number;

    public override void DoIt ()
        Debug.Log ("DoIt ran without passing a GameObject and " + Number + " set.");

    public override void DoIt(GameObject go)
        Debug.Log ("DoIt ran passing a GameObject and " + Number + " set.");

You may notice that there's a CreateAssetMenu tag above this tag will create a menu item where when you click it, it'll create an asset file.

Now if you click on the First Action menu item you'll create an asset which you can name how ever you want.

As you can see you can now edit the Number variable within the inspector, now we just need to code something up that can quickly run the actions. For this, we'll have a basic class with an InputCheck function to do this.

Your class should look like the following:

using UnityEngine;

public class InputStuff : MonoBehaviour {

    public Action Action;

    // Update is called once per frame
    void Update () {

    private void InputCheck()
            Action.DoIt ();
            Action.DoIt (gameObject);

Now if we attach it to something like the camera in the scene and drag the FirstAction asset onto it like so:

If change the number in the asset to two and run it we should get the following:

This is a very, very simple script and doesn't even scratch the surface of what scriptable objects can do as mentioned in the video above AI is one of the ways that these can be used, another is Items/Equipment etc, it's especially good when making tweaks during runtime and want the changes to be persistent since unlike MonoBehaviours any changes made during runtime stay there.

That's that for this tutorial though I probably will touch on ScriptableObjects again in the future.

Any comments and suggestions are very much welcome.