Instantiating a prefab from the library in C# with Unity 3D
Unity 3D is really geared toward visually building your game through use of the Unity 3D editor. However, there are times when you want to instantiate a prefab that you don’t already have in the Scene.
Instantiating a prefab from your library is easy, once you know how. Figuring out how is a pain. The biggest quirk is that your prefab must be located in a directory called “Resources” if you want to instantiate it using C#.
Here are the steps.
1. Make sure you create a directory called “Resources” and place it in your Assets/ directory.
2. Create a prefab. Let’s all it “FooPrefab”.
3. In C#, instantiate the prefab as follows:
GameObject go = Instantiate(Resources.Load("FooPrefab")) as GameObject;
Why does the prefab have to be placed in a directory called “Resources”.