Can somebody give me an example on how this might work or how I’d us this in JS? Please?
I’m getting an error with the following code.
var testNumber = new Array();
function Awake()
{
for(i = 1; i <= 7; i++)
{
testNumber.Push(i);
}
}
//this function is called upon a button press
function CheckNumbers()
{
if(System.Array.IndexOf(testNumber,4) == -1)
{
//DO SOMETHING
}
else
{
//DO SOMETHING ELSE
}
}
I’m receiving the ERROR: BCE0023: No appropriate version of ‘System.Array.IndexOf’ for the argument list ‘(Array, int)’ was found.
Please help…
Array.IndexOf isn’t related to the Array class in JS. Actually you shouldn’t use the Array class at all, use List instead.
import System.Collections.Generic;
var testNumbers : List.<int>;
function Awake()
{
testNumbers = new List.<int>();
for (var i = 1; i <= 7; i++)
{
testNumbers.Add(i);
}
}
//this function is called upon a button press
function CheckNumbers (list : List.<int>, numberToCheck : int)
{
if (list.IndexOf (numberToCheck) == -1)
{
//DO SOMETHING
}
else
{
//DO SOMETHING ELSE
}
}
Although I would suggest Contains instead, which is more appropriate and readable for this usage.
if (list.Contains (numberToCheck))
–Eric