- Home /
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!
Answer by tuinal · Jul 10, 2020 at 11:30 PM
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.
Thanks, I think I'll create a separate static class.
Your answer
![](https://koobas.hobune.stream/wayback/20220613002924im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
multiple extensions with EditorUtility.OpenFilePanel 2 Answers
Unity Editor script in DLL 1 Answer
Unity Editor Extensions 1 Answer
How to add info.plist to notification service extension? 0 Answers
World Builder from Unity Asset Store 3 Answers