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.

more ▼

asked Feb 23, 2010 at 12:49 PM

Dumitru Catalin gravatar image

Dumitru Catalin
196 5 5 9

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?

Jun 05, 2012 at 08:03 PM altra4u
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Feb 23, 2010 at 01:04 PM

Ashkan_gc gravatar image

10.1k 77 110 165

Minor note: Asset Bundles are Pro-only.

Mar 12, 2010 at 04:24 PM Cyclops

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

Apr 24, 2010 at 08:13 PM joeltebbett

your welcome man!

Apr 25, 2010 at 05:45 AM Ashkan_gc

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!

Apr 24, 2012 at 06:27 PM sp00ks

and you dont need to Instantiate it immidately. Load the resource into a GameObject using something like:

GameObject goEnemy = (GameObject)Resources.Load("enemyprefab");

Then you can Instantiate goEnemy as much as you like without "loading".

Mar 28 at 12:29 AM BerggreenDK
(comments are locked)
10|3000 characters needed characters left

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

Create a folder called "Resources"

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

more ▼

answered Sep 11, 2010 at 09:28 PM

user-3794 (google) gravatar image

user-3794 (google)
161 4 14 5

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

Mar 28 at 12:27 AM BerggreenDK
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered May 20, 2012 at 07:08 AM

1337GameDev gravatar image

795 169 136 154

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

Mar 28 at 01:52 PM BerggreenDK
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Feb 23, 2010 at 12:49 PM

Seen: 63993 times

Last Updated: Jul 14 at 03:10 PM