ScriptableObject Unity, When and how to use.

The word ScriptableObject Unity does sound scary but the once you understand what it is, then you might use it on all your Unity projects. In this tutorial, we will see what is ScriptableObject in Unity, it’s advantages and how to use it.

Video Tutorial

What is a ScriptableObject in Unity?

ScriptableObject is a simple class that holds data. You might ask what type of data? Well, it can hold any type of data that you want it to. Then why not create a simple C# script? ScriptableObjects are C# scripts that can be inherited in a simple way. Ok. this way little confusing. Now let’s try to understand Scriptable objects in simple terms with examples.

You have heard about Prefabs in Unity, right? Prefabs are Objects that need to be used more than once in the game and are exactly identical to each other. But what if you need a gameobject that has the same properties but different values. Generally, we create a prefab variant. A better approach to this is to create a ScriptableObject and use them to create the variants. ScriptableObject will hold all the data required for your gameobject variants. You can think of ScriptableObject as a template for your gameobjects.

Still confused? No problem, it will be clear once we see the execution.

Unity Black Friday Deals

Why use a ScriptableObject?

All the data that you store using ScriptableObject is saved as an Asset and is independent of Scenes in Unity. Since they are just data containers, they occupy a very small disk space. They are also good for better memory allocation and performance.

If you create multiple gameobjects with same parameters then it creates a memory slot of each datatype. If you have 1000 gameobjects then it means allocating space for so many variables. But if you use a scriptable object and a gameobject template then you will need to allocate memory once once.

How to create a ScriptableObject in Unity?

Creating a ScriptableObject is very simple. You just need to create a simple C# script inside your project folder. You can do that by right clicking on the Project folder and going to Create>C# script.

Open the script in your script editor and change the inheritance of your class form Monobehaviour to ScriptableObject.

Here is an example for an empty ScriptableObject

using UnityEngine;

public class Hero : ScriptableObject
{
   
}

Adding properties to your ScriptableObject

You can add in any datatype that you want inside the script and save it. Whether to make then public or private will depend on your game’s design. If you just want to read the data then you can make them private and create small functions to read the data.

Let’s create some properties for our hero object. Every hero will have a prefab that’s the model and animation, then Name, Health and Damage.

Also, we need to add an Asset menu option, so that we can create Heros from project window.

using UnityEngine;
[CreateAssetMenu(fileName ="Hero_features",menuName ="ScriptableObject/Hero")]
public class Hero : ScriptableObject
{
    public GameObject hero_model;
    public string hero_name;
    public float hero_health;
    public float hero_damage;
    
   
}

Let’s Create Heros

Now that we have our template in form of a ScriptableObject, let’s create some heros. Just go to the project window, right click>Create>ScriptableObject>Hero.

This will create an GameObject of type Hero. You can set the parameter based on your requirement and you have a Hero ready. I am going to create two Heros Cube and Capsule and assign then different properties.

Now that we have create Heros, what’s next? By now, you should have noticed that nothing happened after creating the Heros. Thats because we need to add them to the scene.

Let’s create a template to add in any type of hero that we need.

Create an empty gameobject in your scene and attach a script called Hero_spawn.

In this script, we will take in the values from our Hero scriptable object and use them to spawn a Hero.

using UnityEngine;

public class Hero_spawn : MonoBehaviour
{
    public Hero hero_data;
    string player_name;
    float player_damage;
    float player_health;
    

    // Start is called before the first frame update
    void Start()
    {
        player_damage=hero_data.hero_damage;
        player_health=hero_data.hero_health;
        player_name=hero_data.hero_name;
        Instantiate(hero_data.hero_model,transform.position,Quaternion.identity);
        Debug.Log("Player Name: "+player_name);        
        Debug.Log("Player Damage: "+player_damage);  
        Debug.Log("Player Health: "+player_health);       

    }

    
}

Now just assign the different Heros that you have created and see them spawn.

Now comes the biggest question. Why not create a prefab with the parameter variants for each Heros, rather than creating scriptable Objects.

Prefabs vs ScriptableObject

You can very well create prefabs in place of ScriptableObjects but it’s just better structured and more efficient to create ScriptableObjects in some cases.

A prefab with a single script attached can do the same thing as a ScriptableObject, except that it has the extra overhead of having a GameObject and Transform so it takes up more disk space, more RAM, more load time, etc. which is a waste┬áif you don’t actually need any of its features.

So if you have 100 Heros in your game and all are prefabs then it will take up more disk space than using a ScriptableObject.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.