So the current practice in Unity is to create a public variable exposed to the editor which references a Prefab from the project panel. Instantiate is then used to create a new object of this type. Code (csharp): public Transform myPrefab; ... GameObject myObject; myObject = (GameObject)MonoBehaviour.Instantiate(myPrefab, new Vector3(0,0,0), Quaternion.identity); I understand that this gives you the ability to change the prefab at any time by dragging a different one to the script. However, is there a way to instantiate a new GameObject just using the class name? E.g: Code (csharp): GameObject myObject = new MyPrefab(); Or even something like: Code (csharp): GameObject myObject; myObject = (GameObject)MonoBehaviour.Instantiate(GetType('MyPrefab'), new Vector3(0,0,0), Quaternion.identity);
You have to put the prefab you want to be able to instantiate from code in your Resources folder. Then you can instantiate in code using Code (csharp): Instantiate(Resources.Load("prefabName")); [edit: Resources folder can be anywhere in your Assets folder.]
Thanks. Quick follow-up question - does this externalise the resource, or is it still contained in the final build?
They're still contained in the final build, in fact, putting assets in the Resources folder forces them to be included wether you use them or not. See http://unity3d.com/support/documentation/ScriptReference/Resources.html for more information.