- Home /
Instantiate prefab wihtout dragging prefab into gameObject script
I have a large volume of prefabs, and I want the user to able to select from a menu different names. These names each correspond to a specific prefab. When this happens, a specific GameObject is selected, and I would like to instantiate a prefab under that GameObject local Transform. (I am building a machine, and for each part there is a list of other possible parts than can be attached, stoed in a database. The part library is rather big. Think Toons or "The Incredible Machine" :-) ).
Later I would like to stream these components, and this is the basis for this problem.
var cube : Transform;
function Start () { for (var y = 0; y < 5; y++) { for (var x = 0; x < 5; x++) { var cube = Instantiate(cube, Vector3 (x, y, 0), Quaternion.identity); } } }
The current code is taken from the instantiation tutorial available at http://unity3d.com/support/documentation/Manual/Instantiating%20Prefabs.html
For this to work, I am required to put the above script into a gameObject, and then Drag the Prefab into the Cube variable slot for that script component in the inspector. This means I have to have access to all the prefabs. I would like to achieve this without the dragging. This is because the plan is to put new assets (prefabs) in my online database as this collection grows, and have the system automatically donwload (from an URL) the prefabs as needed.
Is this possible, or have I completely misunderstood the concept of Prefabs? Is there a way that I can set this variable to a streamed asset using the WWW class? ( I found this thread :http://answers.unity3d.com/questions/17376/reconnect-programmatically-instantiated-prefab but I did not understand it completely, although it might be what I am looking for).
Help is really appreciated.
Answer by AliAzin · Aug 06, 2010 at 05:24 PM
you can export your prefabs to AssetBundles and call your prefabs through there. see Character Customization project.
i can't believe that the Unity people thought that an interesting project for us 20 30 year olds to learn from was dressing up barbies. (off topic) they are not even adult barbies with prober boobs, its dressing up 7 year olds in pink and yellow. TEDIOUS!
By any chance, was the project remade into the "robot lab"?
Answer by Thet Naing Swe · Oct 08, 2010 at 02:29 AM
If your prefabs are located in the Resources Folder, then you can load your prefab in that ways.. it has been tested and works.
C#Version
GameObject testPrefab = (GameObject)Resources.Load("/Prefabs/yourPrefab");
The path should be relative to the Assets/Resources folder. I have only got a success with this:
GroundTile = (GameObject) Resources.Load("Prefabs/$$anonymous$$apTiles/GroundTile", typeof(GameObject));
Answer by pravusjif · Oct 27, 2012 at 11:51 PM
I tried this in several ways but i could only make it work this way:
GameObject codeInstantiatedPrefab = GameObject.Instantiate( Resources.LoadAssetAtPath("Assets/Prefabs/myPrefab.prefab", typeof(GameObject)) ) as GameObject;
Resources.LoadAssetAtPath is for Editor only though. The documentation says,
"Returns a resource at an asset path (Editor Only). This function always return null in the standalone player or web player. This is useful for quickly accessing an asset for use in the editor only."
Answer by Happy-Zomby · Aug 20, 2014 at 09:30 AM
Hi, For the Resources.Load I don't believe you need the prefab extension... just need to make sure you don't have a "/" at the beginning of your path... You can also use a string/list to generate it. E.g. var tempObject = Instantiate(Resources.Load("Prefabs/Units/"+loadData.animalNames[i])) as GameObject;
Answer by Ziron999 · Feb 07, 2014 at 04:31 PM
Resources.Load is the best way to do this...this is how you do it in detail:
Instantiate(Resources.Load<GameObject>("NameOfPrefab"WITHOUT(.prefab)"")) as GameObject;
Folder in assets folder must be created called Resources. Loads on both builds and in editor! No need for assetbundle that is a pro only feature anyway!
LoadAssetAtPath
is only allowed in the editor and won't work in a standalone build or the web player.