targets = GameObject.CreatePrimitive(PrimitiveType.Sphere);
i want to create 10 different gameobjects,not just sphere,such as cube,capsule
is anyone know how to do that?really confused about the array in unity3d
targets = GameObject.CreatePrimitive(PrimitiveType.Sphere);
i want to create 10 different gameobjects,not just sphere,such as cube,capsule
is anyone know how to do that?really confused about the array in unity3d
have a look on this site : http://blog.nobel-joergensen.com/2010/12/25/procedural-generated-mesh-in-unity/
The simplest way, if I understand you correctly, would be:
create a public GameObject array
public GameObject[ ] targets;
In the editor, fill the array with all the primitives you wish to use, or use prefabs if you want more flexibility
Pick an object randomly
int choice = Mathf.RoundToInt(Random.value * targets.Length);
Instantiate the random object
thx for share
i fix that problem already, but i think your code looks much more better.Anyway this is my code:
//The targets
//create 10 random game objects when the programming start
switch (choice)
{
case 1:
targets = GameObject.CreatePrimitive(PrimitiveType.Sphere);
break;
case 2:
targets = GameObject.CreatePrimitive(PrimitiveType.Capsule);
break;
case 3:
targets = GameObject.CreatePrimitive(PrimitiveType.Cube);
break;
default:
Debug.Log(“Something wrong!!”);
break;
}
choice = Random.Range(1, 4);
Your method limits you to only use primitives and it also is static and hard-coded (if you can even say that about a script). An array of GameObject would let you expand and contract the selection of randomly instantiated objects from the inspector without changing code.