Check contents of an Array?

Is there a function to check the contents of an array?

Example:

var box = new Array()

function Awake()
{
     for(var i = 0; i <= 5; i++)
     {
          box.Push(i);
     }
}

function Update()
{
     if(box.???? == 2)
     {
          DoSomething
     }
}

Maybe I’m blind, but I can’t seem to find anything in the API doc.

Don’t use Array, use List, which has things like Contains.

–Eric

you have to search through the array. You can either use another for loop:

for (var i = 0;i<box.Length<i++){
    if (box[i] == 2){
       DoSomething
    }

}

or use the for in syntax:

for (var number : int in box){
     if (number == 2){
         DoSomething
     }

}

It would really be a lot better to use List, so A) you don’t reinvent the wheel by writing array searching code, B) you get much better performance, and C) your code automatically works with #pragma strict.

–Eric

Since when does a List perform better than an array? Though I do notice it’s “Array” and it’s being created as a type… is this some JavaScript specific thing…?

The Array class is not a native array. Native arrays are fast but you have to do all the work for yourself. For someone asking such a basic question its almost certainly better for them to use a List.

Yes, the JS Array class. For a while, JS couldn’t (easily) use List, but since generics were added, there’s no reason to use Array. If you don’t need add/remove items, of course, a fixed-size array is fastest.

–Eric

Gotcha.

The documentation doesn’t help newcomers understand that, and, I feel, steers them in a poor direction. The syntax for generics is slightly more complex-looking, but the page on Arrays was one of the first things I studied, in my programming life, and I don’t think Lists would have seemed any more daunting, given that I didn’t have a great grasp of the concepts, and was largely copying and pasting bits I didn’t understand, until I used them enough to really get it*. I still don’t know what the .< syntax does behind the scenes, but that doesn’t mean I can’t use it. Do you know if any bugs were logged about this? I think Unity’s Array class ought to be deprecated, and only be documented in places that warn learners to not bother with it.

*I distinctly remember feeling that all of this was magic, perhaps specific to Unity, perhaps not:

new Array ();
array.ToBuiltin(Vector3);
new Array (builtinArray)

.ToBuiltin hasn’t ever even properly documented.

The documentation provides enough information to get started, but not enough to overwhelm. I do think that MSDN links are a good idea, but I feel they are too complicated and don’t look enough like code that will compile for Unity to be enough for beginners. If the Unity docs just tell you how to do the same stuff as they currently do, only with Lists instead of arrays, they won’t be any more confusing to the target audience. The level of newness is too great for a . or <> pair to make any difference.

There are actually two reasons one might choose JS arrays:

  1. You need a dynamic array that can hold anything (and can withstand the speed penalty)
  2. You are using a machine that can’t (or won’t) run 3.0 or higher, as generics weren’t added until then.

As to the opener’s original question, UnityScript and Boo support the keyword “in”, and can be used with any container (JS Arrays, .NET Arrays, generic Arrays, etc):

if ("apple" in fruit) //do something

ArrayList is built into the languages Unity supports, and does the same thing, with more functionality. The documentation even points to it.

It was not 3.0. I don’t know when it happened to the non-iPhone Editor (it was either at 2.5 or before), but Unity iPhone, which used the original style, Mac-only Editor, had support for generics in its later days.

Yes, I sometimes use ArrayList instead (I’ve always assumed it’s faster than JS Arrays), but they don’t have the exact same feature set. Pop(), Shift(), and Unshift() are not members of ArrayList.

I still use 2.5 on my Mac laptop (I hacked the drive to Triple Boot, now can’t install Snow Leopard), and it does NOT support generics, unless it uses a different syntax than 3.0 and above.

Well, in that case you’d use a List of Objects. It’s still quite a bit faster than Array even with the casting.

True, but I can’t imagine that applies to many people. :wink: (And yes it was 3.0 that they were added to Unityscript.)

–Eric

Are there cases where you want the features of Array you mentioned, but don’t actually want to be using a Stack or Queue? (If they’d add extension method support for UnityScript, it wouldn’t matter so much.).

Unity 2.0 is where generics were first supported. But not for UnityScript. Sorry.

Honestly, these days I’m mostly using Boo, so I would just use a Boo List for convenience sake.

Would replace with generic list during optimization if needed.