Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by starfighter1314 · Jul 21, 2012 at 04:24 AM · c#instantiateprefabmonobehavior

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image starfighter1314 · Jul 21, 2012 at 07:39 AM 1
Share

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...

avatar image Julien-Lynge · Jul 21, 2012 at 08:49 AM 2
Share

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.

avatar image Bunny83 · Jul 21, 2012 at 11:41 AM 1
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does the instantiation of a prefab work well when the scene is first loaded, but then the cloned GameObjects are no longer displayed in the scene? 0 Answers

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges