# How do we instantiate random sprites from an array...

Hi … i am working on 2d game in which i have flower which is made with 2d petals(sprites)…totals petals are 30… what i am trying to do when my scenes start it randomly pick 20 to 30 petals and instantiate them on their actual position…i know i can do that with array or list … i have searched and tried but its complicated… below is my solution but its not good… first it instantiate random petals but they are not on there actual position … they overlap eachother second it also instantiate the same petals again … in array we can remove the element which already been instantiated so it do not copy again … but when i try it show me error … its an essay task but i don’t know its complicated for me …

``````#pragma strict

var petals : GameObject[ ];
var random: int;
//var location:Vector3= new Vector3(0.9006966,0.5747727,-4);

function Start(){

var random = Mathf.Abs(Random.Range(1,30));
Debug.Log(random);
for (var i:int =1; i<=random; i++){
var petal= petals[Random.Range(0,petals.Length)];
//		if (petal==petals[Petal1])
//		{
////		location:transform=
//		}
var petalClone= Instantiate(petal,transform.position,transform.rotation);
//		Destroy(petals*);*
``````

// petals.RemoveAt(i);

• }*
}

I’m a bit fuzzy on the rotation part of the problem, but I’m going to take a shot at an answer that will get you at least part of the way there. First, rather than try and remove things from the array, just shuffle the array. You do that by walking through the array and randomly swapping elements. As for rotation, assuming the original game objects have the correct rotation, you can use the single parameter version of Instantiate() and then position the petals.

``````#pragma strict

var petals : GameObject[ ];
var random: int;

function Start(){

// Shuffle
for (var k = 0; k < petals.Length; k++) {
var j = Random.Range(0, petals.Length);
var go = petals[k];
petals[k] = petals[j];
petals[j] = go;
}

var amount = Random.Range(20,30);
for (var i = 0; i < amount; i++) {
var petalClone = Instantiate(petals*);*
``````

petalClone.transform.position = transform.position;
}
}