hi, i have a code who make a planar take gravity, and I take a Random number to put gravity in the planar, I have a array of planar and has other array who take the last number of the first random.
I wanna make the random never take a number who has sortied before.
here is my code
var planos : Collider[];
var ultimosSortiado : int[];
ultimosSortiado = new int[17];
private var planoCai : int;
function Update () {
}
function caiChao(){
planoCai = Random.Range(0, 18);
ultimosSortiado[planoCai] = planoCai;
if(ultimosSortiado[planoCai] == planoCai){
planoCai = Random.Range(0, 18);
print("eh");
} planos[planoCai].attachedRigidbody.useGravity = true; > for (var value : int in ultimosSortiado) {
print(value);
}
}
I dont know how i can see inside the array to look if I has the number who I random
Well, you could look through the array to see if a value is there:
for (i = 0; i < ultimosSortiado.Length; i++)
if (ultimosSortiado *== theValue)*
*break;*
*if (i < ultimosSortiado.Length)*
*we know it's there already because it quit before the end of the array*
*```*
*<p>But, there are other ways of doing this.</p>*
*<p>If you want a array of random integers that only get used once, (like an 18-element array that has random numbers from 0-17 in it), you can use a 'shuffle' algorithm. Brute force, it means you generate a random number for each element, then sort the array, keeping track of the indices. The resulting list of sorted indices will be in random order, and used just once of course.</p>*
*<p>But there are other classes which have methods like .Contains() or .Shuffle or whatnot that might be easier to use.</p>*
*<p><a href="http://msdn.microsoft.com/en-us/library/bb384015.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/bb384015.aspx</a></p>*