Classes in Unity (Part 1/3): MonoBehaviour vs Custom Class

Simon Pham
4 min readMar 9, 2024

--

What is a Class?

Simply speaking, a class is a container for variables and functions and is used to group things that work together. Whenever you create a new C# script, Unity automatically creates a class of MonoBehaviour that shares the same name as the script file it is in.

MonoBehaviour vs Custom Class

MonoBehaviour is a base class provided by Unity for creating scripts that can be attached to GameObjects in a Unity scene. It allows developers to implement various lifecycle methods such as Start(), Update(), etc., which are automatically called by the Unity engine at specific points in a GameObject’s lifecycle.

Here’s an example of a MonoBehaviour class:

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

public class Wizard : MonoBehaviour
{

private void Start()
{

}

private void Update()
{

}
}

A custom class in Unity refers to any class created in a C# script outside the MonoBehaviour framework. It is normally used for data storage, calculations, utility functions, or any other logic that doesn’t directly interact with GameObjects or Unity’s lifecycle.

Here’s an example of a custom class:

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

[System.Serializable]
public class Spell
{
public string name;
public int levelRequired;
public int itemIdRequired;
public int expGained;
}

How to create and use a custom class in Unity?

Let’s create a custom class for spells in a wizard-themed video game. I’m going to create a script called “Spell” that acts as a blueprint for all of the spells we have in the game. By default, Unity will create a MonoBehaviour class for the Spell script:

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

public class Spell : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

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

}
}

We’d need to delete the MonoBehaviour keyword and the Start and Update methods.

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

public class Spell
{

}

Next, I want all the spells to have a name, level, item required to cast them, and experience gained for casting them, so I’m going to create four variables. Also, I added the [System.Serializable] attribute to make these variables editable in the Unity editor:

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

[System.Serializable]
public class Spell
{
public string name;
public int levelRequired;
public int itemIdRequired;
public int expGained;
}

After that, let’s create a constructor so that we can create new spells and a method to cast them:

public Spell(string name, int levelRequired, int itemIdRequired, int expGained)
{
this.name = name;
this.levelRequired = levelRequired;
this.itemIdRequired = itemIdRequired;
this.expGained = expGained;
}

public void Cast()
{
Debug.Log("Casting " + this.name);
}

And here’s the full code:

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

[System.Serializable]
public class Spell
{
public string name;
public int levelRequired;
public int itemIdRequired;
public int expGained;

public Spell(string name, int levelRequired, int itemIdRequired, int expGained)
{
this.name = name;
this.levelRequired = levelRequired;
this.itemIdRequired = itemIdRequired;
this.expGained = expGained;
}

public void Cast()
{
Debug.Log("Casting " + this.name);
}
}

After we’ve finished creating a custom class for spells, let’s create a new MonoBehaviour script called “Wizard” to utilize the custom class we’ve just created, and I’ll attach it to a GameObject in the scene:

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

public class Wizard : MonoBehaviour
{
public Spell[] spells;

public int level = 1;
public int exp;
}

So I’ve added a variable for level, experience, and an array for all the spells that the player can cast.

Next, I want the character to cast a spell when the space key is pressed.

private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
foreach(var spell in spells)
{
if (spell.levelRequired == level)
{
spell.Cast();
exp += spell.expGained;
}
}
}
}

The final step is to populate the spells in the editor.

And you should be able to cast the spell that matches the character’s current level.

--

--

No responses yet