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.

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");

you can export your prefabs to AssetBundles and call your prefabs through there. see Character Customization project.

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!

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*)) as GameObject;*

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;

You can get help from this tutorial

This is what you are looking for:

PrefabUtility.InstantiatePrefab()

I don’t know if this is right but it worked for me:

Instantiate(AssetDatabase.LoadAssetAtPath(“Assets/MyPrefab.prefab”, GameObject));

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;