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.
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?
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.
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.
Minor note: Asset Bundles are Pro-only.
You've just saved me A LOT of time with this method, Ashkan :D
your welcome man!
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!
Just keep in mind the following:
"In Unity you usually don't use path names to access assets, instead 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 minimizes 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."
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"));
You dont need to instantiate immidiately. Rather store it in a variable for future spawns...
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.
@1337GameDev - nice, didnt know that one. Thanks for sharing.
Fairly irrelevant to OP's question.
Answer by el_santo
Mar 25, 2015 at 03:24 PM
Object prefab = AssetDatabase.LoadAssetAtPath("Assets/something.prefab", typeof(GameObject));
GameObject clone = Instantiate(prefab, Vector3.zero, Quaternion.identity) as GameObject;
clone.transform.position = Vector3.one;
Don't do this. (Unless you're developing assets for the Asset Store or something.) AssetDatabase is a UnityEditor class. I.e. it won't work anywhere outside of the editor.
@b1nary_atr0phy, yes, you are right. I forgot that used UnityEditor. Find mistake only then try build Android version.
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"));
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
8 People are following this question.
Problem with Camera instantiating
NullReferenceException from a script after linking prefab with drag and drop in inspector. WTF?
How to create new tail in a simple 3D Snake
Trying to assign GUIText from heirarchy into instantiated prefab