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.

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.

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

Create a folder called "Resources"

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

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.

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

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;

Object prefab = AssetDatabase.LoadAssetAtPath(“Assets/something.prefab”, typeof(GameObject));
GameObject clone = Instantiate(prefab, Vector3.zero, Quaternion.identity) as GameObject;
clone.transform.position = Vector3.one;

Source: http://forum.unity3d.com/threads/editor-script-create-game-object-from-a-prefab.47845/#post-304595