- Home /
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[i]);
// petals.RemoveAt(i);
}
}
Answer by robertbu · Apr 24, 2014 at 03:00 PM
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[i]);
petalClone.transform.position = transform.position;
}
}
@robertbu Thanks for the answer... its good but the number of petals won't change and the shuffling part do not work as well in my case because it will change the shape of the flower as well ... what i am trying to do is instantiate the petals on their actual position but just every time when user start the scene change the number of petals.... number of petals is working in my script but there position and instantiate same petal is not .. what i though , when my loop runs, in first loop, pick one petal from the array, instantiate it and remove it from the array so in the second loop it won't repeat.... i tried to use Destroy and RemoveAt it should have worked but may be my bad luck :( .. in my game i am restricted to have different number of petals every time ... ;)
I'm having trouble understanding why shuffling change the shape, and line 16 changes the amount in the range of 20 to 30. But you know what you want. You cannot remove entries from a built-in array. What you have to do is use a .NET list. This is essentially what you describe:
#pragma strict
import System.Collections.Generic;
var petals : GameObject[ ];
var random: int;
function Start(){
var petalList : List.<GameObject> = new List.<GameObject>(petals);
var amount = Random.Range(1,30);
for (var i = 0; i < amount; i++) {
var j = Random.Range(0, petalList.Count);
var petalClone = Instantiate(petalList[j]);
petalClone.transform.position = transform.position;
petalList.RemoveAt(j);
}
}
But this code will also shuffle the positions. I'm guessing the heart of your issue is that you want all the petals to be instantiated in the same order they appear in the array...just have some of the petals missing. Here is yet another way to approach the problem. What it does it remove (by marking them null) petals from the original list to get down to a specific count. Then it instantiates any non-null entries.
#pragma strict
var petals : GameObject[ ];
var random: int;
var $$anonymous$$Petals = 5;
var maxPetals = 20;
function Start(){
var wanted = Random.Range($$anonymous$$Petals, maxPetals);
var discard = petals.Length - wanted;
if (wanted >= petals.Length) {
Debug.Log("maxPetals must be less than the count of petals!");
return;
}
while (discard > 0) {
var i = Random.Range(0, petals.Length);
if (petals[i] != null) {
petals[i] = null;
discard--;
}
}
for (var j = 0; j < petals.Length; j++) {
if (petals[j] != null) {
var petalClone = Instantiate(petals[j]);
petalClone.transform.position = transform.position;
}
}
}