trying to avoid Implicit Downcast while using Array()

Hello!

I have a simple little problem, i am sure most of you would laugh at me. I guess i simply don’t know some syntax here.

I am working on a 2D game, so i am using a lot of Vector2-s. When i store multiple Vector2-s in an array, like Vector2[ ], they don’t lose their datatype. But for some implementation i am using the ‘Javascript Array’ instead, the one you create with new Array(), because they have dynamic length.

So, when i am iterating through my javascript Array called ‘points’, it turns out that it contains objects, not Vector2-s. They print out just fine, but i cannot access their properties as ‘.x’ and ‘.y’. So i do this to every element before processing:

var vect:Vector2 = points[i];

It runs just fine, but i get this warning:

BCW0028: WARNING: Implicit downcast from 'Object' to 'UnityEngine.Vector2'.

I would happily downcast it in a proper way, like accessing the coordinates separately from ‘points*’, but i seem to find no way to do it. Both*
* *print(points[i].x); print(points[i][0]);* *
give me errors.
* *print(points[i]);* *
just wors fine, outputting the element like that:
* *(20.0, 1.0) UnityEngine.MonoBehaviour:print(Object)* *
Please help, how can i separately address the values in an Object like that? Also, why do Vector2-s become Objects when put into an Array in the first place?

Use a List instead of the UnityScript array. You find a comparison of the different containers here, along with some examples:
http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use%3F

Because Array only contains Objects. Never use Array, use generic Lists as Dantus mentioned. (Or built-in arrays if the size of the array doesn’t change.)

–Eric

If i cannot solve it any other way, i might change to another data type, but the theoretical question remains:

Is there a way to access the individual properties of Objects? Sure the engine can access them, as printing, and downcasting works.

I would very strongly suggest not using Array. It’s obsolete and not worth bothering with. If you want to get rid of the warning you can use #pragma downcast. But seriously, just don’t ever use Array.

–Eric