# Compare Contents of arrays

Alright the problem here is i want my array to be a int[]. The problem is I do not know how to compare one number to all the contents of a array. Here is my code:

``````var randomNum : int;
var randomNum2 : int;
var compareNumber : int[];
var constantNum : int = 0;
var once : boolean = true;
var brick : Transform;
var rowOne : Transform[];
var rowTwo : Transform[];

function Start () {

}

function Update () {
if (once) {
randomNum = Random.Range(1,5);
if (randomNum > 0) {
if(randomNum2 == compareNumber) {
randomNum2 = Random.Range(1,rowOne.length);
compareNumber[constantNum] = randomNum2;
Instantiate(brick, rowOne[randomNum2].transform.position, Quaternion.identity);
constantNum++;
} else {
randomNum2 = Random.Range(1,rowOne.length);
}
randomNum--;
}
once = false;
}
}

``````

The problem is the line: if(randomNum2 == compareNumber) {...well i KNOW it is wrong, thus the problem. What i want is IF the 2nd random number IS in the array it picks a new random number. The correct syntax (at least what i want) is:

``````if(randomNum2 !== compareNumber) {

``````

The problem is that compareNumber is a int[] so i can not see if it not equal to it, or so unity says. So how do i got about having it compare to the contents of the array? Sorry for this sort of question, i am new to arrays and i want to start using them because they are just so useful, just complex i guess.

So something like

``````if( System.Array.IndexOf( compareNumber, randomNum2 ) == -1 )
{
// it doesn't exist in the array
}

``````

Maybe i'm reading it wrong and it is late, but even if you get the compare working is this code correct? I ask because it doesn't seem to make a lot of sense to me.

Your first time through 'once' is true, so it generates a positive random number, which means the conditional check is always true. You then want to compare randomNum2 against your array, but according to the code you posted, its yet to be initialise properly, though i'm not sure if JS will default it to a value or null. If its null i'd expect the compare to error out.

Next you create a completely new randomNum2 in the range of the number of transforms you've added to rowOne, which seems odd considering you were checking to make sure the original randomNum2 wasn't in the previous used array.

Then you assign the new randomNum2 to the next (constanNum) index in the compareNumber array, presumably in order not to select it again. However since its no longer the same randomNum2 as you did the compare with you have no gaurantee that it is a number that hasn't previously been used.

The new randonNum2 is then used as an index to choose a 'random' transform from your array, before incrementing constantNum.

Finally you decrement randonNum by one, as though you want to generate up to the original randonNum 'bricks', yet it is irrelevant as next time into update() it will be recalculated anyway, but that doesn't matter as lastly you set 'once' to false so this chunk of code want be called again at all next time through anyway.

What is it exactly that you want to achieve here? I don't know maybe the code makes sense if there are other functions, but it looks odd to me. If you want to instantiate a random number of bricks with random transforms then there are easier ways.

For example, create a random number (randNUM ) to determine how many bricks to initiate. Use this value to build an array with consecutive numbers from 0 to (randNUM -1). Then iterate over the array using a random number as the index (range would initially be 0 to randNUM -1), take the value at that index and push it into a new array, delete the original value from the original array and decrease the random range by 1 for the next random index.

That way you can build a random array of index values from 0 to randonNumber -1.

Then its simply a case of iterating through the array using the values as indices into your rowOne array to get random transforms.