[C#] Instantiating via class references (Need help understanding)

After watching several scripting tutorials on YouTube, something I’m having a hard time grasping is when people use class references (rather than GameObject or Transform references) to instantiate objects. I realize this is a bit vague, so I’ll give an example below.

Rather than instantiating prefabs this way:

public GameObject enemy;

void Start()
{
   Instantiate(enemy, transform.position, transform.rotation);
}

They’ll instead instantiate them this way:

public EnemyClass enemy;

void Start()
{
   Instantiate(enemy, transform.position, transform.rotation);
}

They ultimately still drag the prefab into the public variable’s Inspector slot, but I’m having a tough time understanding why exactly this works.

I’ve always seen scripts as nothing more than pieces of code that you attach to objects, and that are therefore totally unrelated to those objects’ transforms, materials, and so on and so forth. So why, when you Instantiate with a class reference rather than a GameObject or even Transform reference, does the entire object instantiate–script, transform, and all!?

If someone could break this down for me or help me better understand why this works and why you would ever take this approach instead of the traditional GameObject or Transform variable/reference, I would greatly appreciate it!

OK, when you create a new class (in c# at least), you’ll see it looks something like

public class MyClass : Monobehavior

This means that MyClass inherits some functionality from Monobehavior. This is known as Inheritance and it’s well worth reading up on it.

The inheritance model means that whilst it may look like I’m Instantiating ‘MyScript’, what I’m actually doing is instantiating a Monobehaviour, which for the purposes of this we’ll say is the same as a gameObject. It just so happens that if you instantiate MyScript, you know that the gameobject you create will have the MyScript component attached to it.

So if Instantiating MyScript is the same as Instantiating a GameObject then why do it? Well, for a few reasons.

  1. If you have a member variable public MyScript myScript, within the editor you can only drag prefabs objects to this which have the MyScript component attached - think of this as a sort of validation to help you make sure you put the right things in the right places
  2. If you instantiate MyScript, you immediately have access to MyScript. You don’t need to do something like obj.GetComponent(), which is slow as you already have that reference when you instantiated.
  3. Good practice. It’s always a good idea to know what your gameobjects are and what you expect out of them!

That is a very brief overview and there is a lot more complexity which I brushed over / simplified for the sake of grasping the concepts. As I said, it’s well worth reading about Inheritance. Looking at the Monobehaviour docs is a good idea too: Unity - Scripting API: MonoBehaviour