- Home /
Question by
RussellFincher · Sep 30, 2012 at 05:38 AM ·
javascriptinsantiate
Instantiating a variety of meshes
I need to set up a single scene where one of five meshes appears when the scene starts. A variable is storing a number between 1 and 5 in a scene manager script before the scene loads, and the names of the meshes are numbered 1 through 5. How can I configure my scene so the mesh that corresponds with the variable is instantiated into the scene?
Not necessarily looking for specific scripting advice, but more general advice about how to set this up.
Comment
Best Answer
Answer by save · Sep 30, 2012 at 06:21 AM
You could use an array of objects,
var meshes : GameObject[] = new GameObject[5]; //Assign your prefabs in inspector
function Start () {
Instantiate(meshes[Random.Range(0,5)], Vector3.zero, Quaternion.identity);
}
Instead of Random.Range() you would call SceneManager.meshNumber. Setting that number could be like this,
SceneManager.meshNumber = Application.loadedLevel;
or
switch (Application.loadedLevelName) {
case "Level0": SceneManager.meshNumber = 0; break;
case "Level1": SceneManager.meshNumber = 1; break;
case "Level2": SceneManager.meshNumber = 2; break;
}