Using an element of an array of integers as an integer

I’m having trouble comparing an integer to an element of an array filled with what I think are integers. Can someone tell me how to do this?

I have an array that I fill with what I think are integers:

var lengthArray : Array = [array1.length, array2,length];

Then I create the integer:

var intValue = parseInt(Random.Range(0, 17));

But when I do the comparison

intValue > lengthArray[0]

I get the error:

Operator ‘>’ cannot be used with a left hand side of type ‘int’ and a right hand side of type ‘Object’.

How can I cast the right hand side into an integer?

Either you’re using Unity iPhone, or #pragma strict, because that should work fine normally. Simplest thing is not to use #pragma strict, which is not possible on Unity iPhone of course.

Otherwise you have to cast manually, since, like the error says, you can’t compare variables of two different types (without #pragma strict, Unity does the casting for you at run-time):

var arrayValue : int = lengthArray[0];
if (intValue > arrayValue) {

Or you can use an array of integers instead of the generic Array, which is faster anyway and won’t require extra casting, but less flexible.

BTW, you don’t need parseInt when using Random.Range with integer values.

var intValue = Random.Range(0, 17);

is fine, because Random.Range returns an int in this case.

–Eric

Eric5h5, thanks! That did the trick.