Cannot use Instantiate() in my own class.

I defined a class which doesn’t inherit from Object, like this:

class MyClass{ 
    GameObject prefab;
    void DoSomething() { 
        GameObject newObject = Instantiate(prefab);
    }
}

Then compilation error occurs: Cannot find the declaration of Instantiate().
So why can’t I use instantiate in my own class?
Thanks in advance :slight_smile:

You would also need to write:

GameObject newObject = Object.Instantiate(prefab);

@Bubble233

Because Instantiate is a method of Object class in UnityEngine. You need to write Using UnityEngine; at the top of your script to access Instantiate method

Instantiate is a method implemented in UnityEngine.Object so to access it you will have to write either:

UnityEngine.Object.Instantiate(prefab);
// The 'UnityEngine.' part is just needed if you are using System namespace so it doesn't confuse it with System.Object

or

GameObject.Instantiate(prefab);