- Home /
C# Instantiate Prefab from Class That Does not Inherit from MonoBehavior
Sorry if this is a newbie question, but I cannot find the answer in the forums.
I want to instantiate a prefab in C# from a class that does not inherit from MonoBehavior. For example, I have some view classes in a hierarchy like this:
City -> Rooms -> Walls
City, Room, and Wall are view classes created using "new", but I want the Wall class to have a prefab gameobject associated with it so it can be drawn in the scene. How do I instantiate a prefab from this view class since it does not inherit from MonoBehavior?
Thanks, n
Answer by Julien-Lynge · Jul 21, 2012 at 04:42 AM
Just so I understand this correctly: you have a base class that's not a monobehaviour, and you want to instantiate a prefab, which you will then assign to a variable of another object that you're creating with the new keyword.
Assuming that's the case, you can always access MonoBehaviour functions by calling the MonoBehaviour class directly, like so (pseudocode):
Obj = MonoBehaviour.Instantiate(object);
The calling class doesn't have to inherit from MonoBehaviour for this to work.
Thank you, this worked great. However, is this question an odd one in the Unity group? Is there any documentation regarding utilizing an $$anonymous$$VC approach with Unity? How are other people coding C# views that may or may not instantiate a game object? I thought classes that use $$anonymous$$onoBehavior are only supposed to be attached to game objects...
You're absolutely correct, $$anonymous$$onoBehaviour classes can't be created with the new keyword, they can only be attached as Components to a GameObject. I'm not sure about any documentation with $$anonymous$$VC, but I tend to use $$anonymous$$onoBehaviour-inherited classes when I need access to Unity-specific events (like Update and Start), as well as keeping track of objects in-scene. Beyond that, singletons and non-monobehaviour classes work fine in Unity, and you can freely communicate between the two. If you need to instantiate GameObjects, keep track of the GameObject (or it's Transform) through variables or find it as needed with the myriad Find* methods.
Oh, and if this answer solves your question, be sure to check it as correct to remove it from the queue.
The confusion comes from the fact that Instantiate is a static function of UnityEngine.Object. Every internal Unity-object is derived from this "base object", so is $$anonymous$$onoBehaviour. That means inside of those classes you can use Instantiate directly since it belongs to the class itself.
In a "normal" class (derived from System.Object), your class isn't derived from UnityEngine.Object and therefore your class doesn't have the Instantiate function. This is a basic concept that's valid even outside of Unity in normal C# applications.
However, since it's a static function you can use any class that is derived from UnityEngine.Object to access the function, like Julien showed.
Answer by mindlube · Jul 21, 2012 at 04:39 AM
All those classes need to inherit from Monobehavior, if you want to draw any of them in the scene. Prefabs: "are a collection of predefined GameObjects & Components that are re-usable throughout your game." Alternately you could have a WallComponent class which subclasses MonoBehavior, and is is composed with an instance variable of Wall type. But that sounds like a lot of work.
Your answer
Follow this Question
Related Questions
The prefab you want to instantiate is null. 2 Answers
Issues with instantiated prefabs 1 Answer
Spawning a prefab at another object's location 3 Answers
Issue Instantiating prefab in C# 0 Answers