instantiate random number of prefabs

hi

can any one say how to instantiate random number of clones of a prefab ex… i am having a prefab i want to instantiate that prefab in random number it should be instantiate 2 to 3 clones each time when it get instantiated

[Random.Range][1] will give you a random number between two values, then use a for loop to instantiate the clones. [1]: http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html

ya i know to use random.range to set position but not to instantiate prefab can you please explain it

This question has been asked and answered numerous times. Try google searching please. As for your code. What makes you think you can use the '[]' operator on a GameObject? You'd want a loop..

1 Answer

1

This should do it:

for (var i = 0, i < Random.Range(2,4), i++){
    <instantiate prefab here>
    }

@violence - you are mixing floats and integer here. Suggest: for (var i = 0; i < Random.Range(2,4); i++){ The integer version of Random.Range() is exclusive of the highest value.

Well.. Random.Range(2,4) would be better, since it'll choose ints and not float values.

errors are occurring by using following code var brick : GameObject; function Start () { for (var i = 0, i + 1, i < Random.Range(2.0,3.0)){ var object = Instantiate(brick, Vector3(transform.position.x,-1.23,transform.position.z),Quaternion.identity); object.transform.parent = transform; } }

@santosh810666 did you read robertbu's and flavius's comments? I edited my answer, I made an error in comparing an int to a float. Use the updated code and post the error if it remains.

I should be: for (var i = 0; i < Random.Range(2,4); i++){