Extend unity's classes

Hello I’ve a simple question.

I want to create a static method that does this:

**public static void MyCustomInstantiate()

{

                  // Do stuff

}
**

And I want it to be able to call from

GameObject.MyCustomInstantiate();

Is that possible?
Thanks!

You can’t add to a class like that without modifying it’s source code. You would hypothetically need to make a subclass of GameObject that has your own methods or overrides methods in the base class.

However, GameObject is deliberately sealed to prevent you doing this since it’s so intrinsically linked to the engine (e.g. every engine function would need to then deal with a GameObject being a TypeOf your subclass), and I struggle to think of a legitimate use-case where you’d need to extend it like this.

Instead you can customise any use of GameObject.Instantiate() by assigning the instantiated object, e.g.

GameObject myObject = GameObject.Instantiate(myPrefab);

Then using GetComponent<> etc. on myObject to customise properties. This is the ‘normal’ way of customising a given spawn.