Cannot cast from source type to destination type

Hi guys, so currently in my script Im just trying to get every gameobject within an array to do something (not yet determined, so dont worry about the commented out bit), but my for statement is returning an error, only at runtime-
InvalidCastException: Cannot cast from source type to destination type.

heres the code

function Start () {
var MyArray = new Array(GameObject.FindGameObjectsWithTag("Bad"));
var MyIndex = Random.Range(0, MyArray.length);
Debug.Log(MyArray[MyIndex]);
Debug.Log(MyArray[2]);
var MyArrayStorage = new Array (GameObject);
MyArrayStorage.Add(MyArray[MyIndex]);
Debug.Log (MyArrayStorage);

for(var item : GameObject in MyArrayStorage){
       // item.SetActive (false);
}

}

No doubt someone will also complain about my use of JS array, and so if you can suggest a better way to do this kind of array, that’d be a great bonus, but obviously the main issue is the error

thanks guys

Right, don’t use Array. It’s bad…just don’t. Use a generic List instead. Also, make sure you format your code properly when posting; mostly that’s impossible to read.

You seem either reluctant or unable to implement @Eric5h5’s suggestion, but it really would solve your problem…

#pragma strict
import System.Collections.Generic;

 function Start () {
   var MyArray : GameObject[] = GameObject.FindGameObjectsWithTag("Bad");
   var MyIndex : int = Random.Range(0, MyArray.length);
   Debug.Log(MyArray[MyIndex]);
   Debug.Log(MyArray[2]);
   var MyArrayStorage : List.<GameObject> = new List.<GameObject>();
   MyArrayStorage.Add(MyArray[MyIndex]);
   Debug.Log (MyArrayStorage);
 
   for(var item : GameObject in MyArrayStorage){
         item.SetActive (false);
   }
 
 }