implicit downcast from object to int when using Array

Nobody ever answered that question, though many “answers” were marked as Best Answer.
The question is: WHY does that error pop.

var enemyName: String[];
function Start()
         var myArray = new Array();
         for(var i = 0; i < 5; i++)

What’s wrong with that? Some people said need to declare Array type. But HOW? There’s no documentation on that one.
Is it like “new Array(String)” or smth?
Please no smart comments about WHY am I interested in that exact array definition, I need exactly that one, not List or whatever. It’s “buggy and slow”, I don’t care I need to figure.
Thank you.

Your code in question does not include the line with the implicit downcast. It’s most likely this line (from your comment):

HP[i2] =  hpArray[i2];

HP is an actual integer array. So it’s a strongly typed array. Your “hpArray” is an untyped slow Unityscript “Array” class instance. The Array class can only store “objects”. So whenever you want to use whatever you stored inside the array, it need to be casted into the actual type.

UnityScript is a bit wanky when it comes to casting. Usually UnityScript automatically cast the types since it doesn’t have an explicit casting operator, only the as-cast which only works for reference types but not for value types (like int). What might work is this:

var tmp : int = hpArray[i2];
HP[i2] = tmp;

You said you don’t want to hear anything about “Do not use Array” but there literally no reason to use it at all. You can simply use a List. A generic List provides the same functionality as the Array class but is strongly typed and faster since it doesn’t require any casting (or boxing in case of value types).

To use a generic List in UnityScript you just need to add:

import System.Collections.Generic;
// ...
var hpArray = new List.<int>();

instead of “push” you use “Add”. RemoveAt does exist in the List as well. To convert the List into a normal typed array you can simply use ToArray at the end so you don’t need the loop at all:

HP = hpArray.ToArray();

Array initialization would like this:

var myArray = new String[elementcount];

In your case that’s:

var myArray = new String[enemyName.Length];

Furthermore, arrays don’t know push, but instead:

myArray _= enemyNames*;*_