I was following this Lynda Siding puzzle Flash game development tutorial and it mention about a function called the “indexOf” which was used to check whether a certain variable is inside an array (if it’s inside, it’ll return 0, else -1, things like that).
Is there this kind of function in Unity? Cuz I’ve been translating the codes from AS3 to Java (Unity) for a few days now, but was pause in the process due to the “indexOf” thing…
I tried to use the FOR statement to imitate the function but it couldn’t get as advance as the one in AS3… sigh…
If Unity doesn’t have the same thing, does anyone know how I could code out the same function?
so… how exactly do I use it in Unity javaScript?
var foo = "abcdefg";
Debug.Log (foo.IndexOf("c"[0]));
–Eric
what if I wanted to use it in an IF statement, and looking for the index in an Array?
I tried just now but it gives me an error: ‘IndexOf’ is not a member of ‘Array’.
Here’s what I’ve tried: print(blockArr.IndexOf(blockXPos));
You’ll probably have to write your own.
function IndexOf( arg, array : Array ) : int {
for ( var ix : int = 0; ix < array.length; ix++ ) {
if ( arg == array[ix] ) return ix;
}
return -1;
}
Oops, that was for a string.
var foo = ["blah", "abc", "zzz"];
Debug.Log (System.Array.IndexOf(foo, "abc"));
–Eric
so the parameter for the IndexOf function can only be an Array and a String? I was planning to make it look for an integer in the array…
var foo = [1, 2, 3];
Debug.Log (System.Array.IndexOf(foo, 2));
–Eric
thanks… actually the last question was sorta stupid because I forgot my array was a 2 dimensional array…