Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
24
Question by Dumitru Catalin · Feb 23, 2010 at 12:49 PM · instantiateprefab

Instantiate a prefab through code in C#.

Is there a way to instantiate a prefab, in code through C#, but without actually having to create a public variable of Type GameObject and dragging and dropping a prefab from the project assets into the inspector?

I need to get a reference to an asset, and then instantiate it through code, but I don't know how to do that exactly. I have a game-manager class, which controls the way object are spawned and destroyed, with a total so far of over 20 types of prefabs. It would be far less messy If I could get a reference to them through code than having 20+ public variables in the inspector.

Comment
Add comment · Show 1
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 altra4u · Jun 05, 2012 at 08:03 PM 0
Share

hey guys i'm trying to do something similar. basically I want my artists to be able to export the many different character models along with their body parts. Then I want to be able to build the character in-game. Question is, does enemy need to be a manually created prefab? For me that would mean a lot of manual work. I just want to be able to create the character directly from the fbx files. Possible?

6 Replies

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

Answer by Ashkan_gc · Feb 23, 2010 at 01:04 PM

you can use resource folders. create a folder called resources and put all assets that you want to get reference to them by their names, in that folder. use the Load method to load resources. example let's say you have a prefab inside a resource folder called enemy and you want to instantiate it.

var instance : GameObject = Instantiate(Resources.Load("enemy"));

you can create asset bundles of your prefabs and put them inside your game folder too. then call www("file:/path") and get a reference to them and instantiate them. there is another way too. you can create a class that keeps a list of all prefabs in a dictionary. the name of those prefabs in the dictionary could be your key and the real GameObject reference could be the value.

Comment
Add comment · Show 6 · 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 Cyclops · Mar 12, 2010 at 04:24 PM 0
Share

$$anonymous$$inor note: Asset Bundles are Pro-only.

avatar image JoelAtMoodkie · Apr 24, 2010 at 08:13 PM 0
Share

You've just saved me A LOT of time with this method, Ashkan :D

avatar image Ashkan_gc · Apr 25, 2010 at 05:45 AM 0
Share

your welcome man!

avatar image sp00ks · Apr 24, 2012 at 06:27 PM 0
Share

Thanks a bunch! I really only like using the editor for testing AI, controls that sort of thing. I find building a level kind of a pain, and would rather load and place everything from code. This is great!

avatar image brimock · Nov 08, 2013 at 10:23 PM 4
Share

Just keep in $$anonymous$$d the following:

"In Unity you usually don't use path names to access assets, ins$$anonymous$$d you expose a reference to an asset by declaring a member-variable, and then assign it in the inspector. When using this technique Unity can automatically calculate which assets are used when building a player. This radically $$anonymous$$imizes the size of your players to the assets that you actually use in the built game. When you place assets in "Resources" folders this can not be done, thus all assets in the "Resources" folders will be included in a build."

http://docs.unity3d.com/Documentation/ScriptReference/Resources.html

Show more comments
avatar image
21

Answer by user-3794 (google) · Sep 11, 2010 at 09:28 PM

Slight update to Ashkan's totally correct answer (this time in C#)

Create a folder called "Resources"

GameObject monster = (GameObject)Instantiate(Resources.Load("enemy"));

Comment
Add comment · Show 1 · 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 BerggreenDK · Mar 28, 2014 at 12:27 AM 1
Share

You dont need to instantiate immidiately. Rather store it in a variable for future spawns...

avatar image
5

Answer by 1337GameDev · May 20, 2012 at 07:08 AM

Remember to keep the resources folders organized. You can created a Resources folder anywhere in assets folder, (doesnt have to be a base folder in assets). So you can still have folders to organize your prefabs and then put a Resources folder in the unique folder and put your prefab in that.

Comment
Add comment · Show 2 · 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 BerggreenDK · Mar 28, 2014 at 01:52 PM 1
Share

@1337GameDev - nice, didnt know that one. Thanks for sharing.

avatar image atr0phy · Mar 31, 2015 at 04:21 AM 0
Share

Fairly irrelevant to OP's question.

avatar image
3

Answer by Quatum1000 · Jul 30, 2017 at 02:23 AM

If you like to have a real blue linked prefab created, you should use:

GameObject go = PrefabUtility.InstantiatePrefab(Resources.Load("NameOfPrefab")) as GameObject;

or

public GameObject prefab;
GameObject go = PrefabUtility.InstantiatePrefab(prefab) as GameObject;

Comment
Add comment · Show 1 · 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 unity_WPzfLehdpNGaDQ · Aug 20, 2019 at 08:05 AM 0
Share

That's what I was looking for! Thx man!

avatar image
2

Answer by Digital_Shadow · Dec 14, 2016 at 03:16 PM

Got mine to work by creating folder in assests called "Resources", placing all prefab objects there, and using this c# code:

 GameObject foo = GameObject.Instantiate((GameObject)Resources.Load("prefabName"));
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
  • 1
  • 2
  • ›

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

10 People are following this question.

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

Related Questions

How do i Instantiate a prefab with specific assests included 3 Answers

Prefab and creating objects in script help 1 Answer

Accessing prefabs 1 Answer

Can't move instantiated prefab 2 Answers

Trying to assign GUIText from heirarchy into instantiated prefab 1 Answer


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