Scriptable Objects in Unity

How to create a Scriptable Object?
First of all, we need to create a new script that derives from the ScriptableObject instead of MonoBehaviour:

[CreateAssetMenu(fileName = "enemy", menuName = "Create an enemy")]
public class Enemy : ScriptableObject
    public new string name;
    public Sprite artwork;
    public int attack;
    public int health;
    public int mana;

We added the first line so that we can create a scripable object through the Create menu.
We can click on newly created object and add information in the Inspector window

Now we can use enemy scriptable object to display information on the screen. We need to assign values from the scriptable object to the fields on the Scene. In order to do so, we create another Script where we will assign all those values and make references to the objects on the Scene.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class EnemyCard : MonoBehaviour
    public Enemy enemy;
    public Text txt_name;
    public Text txt_attack, txt_health, txt_mana;
    public Image img_artwork;

    void Start()
        txt_name.text =;
        txt_attack.text = enemy.attack.ToString();
        txt_health.text =;
        txt_mana.text = enemy.mana.ToString();
        img_artwork.sprite = enemy.artwork;

Leave a Comment

Scroll to Top