"InvalidCastException" with MeshFilter from JS Array to builtin array.

I apologise, if my problem will not turn to be of common interest, but I try to repeat what is suggested by a piece of code in Unity Scr. Ref. Moreover, I understand that I am limited by significant lack of knowledge in coding and I’m not sure if the data I am providing are enough to locate the problem. I searched for a while in similar error cases, but I don’t think I can figure out what’s going on with mine.

Well, similarly to this Unity example:

var array = new Array (Vector3(0, 0, 0), Vector3(0, 0, 1));
array.Push(Vector3(0, 0, 2));
array.Push(Vector3(0, 0, 3));
 // Copy the js array into a builtin array
var builtinArray : Vector3[] = array.ToBuiltin(Vector3) as Vector3[];

I do the following:

var unappliedMeshes = new Array(MeshFilter);
unappliedMeshes.Push(unappliedMesh); //unappliedMesh is a MeshFilter
var builtinUnappliedMeshes : MeshFilter[] = unappliedMeshes.ToBuiltin(MeshFilter) as MeshFilter[];

But I get the following error message:

InvalidCastException: Cannot cast from
source type to destination type.
System.Array.Copy (System.Array
sourceArray, Int32 sourceIndex,
System.Array destinationArray, Int32
destinationIndex, Int32 length) (at
/Applications/buildAgent/work/b59ae78cff80e584/mcs/class/corlib/System/Array.cs:997)
System.Array.Copy (System.Array
sourceArray, System.Array
destinationArray, Int32 length) (at
/Applications/buildAgent/work/b59ae78cff80e584/mcs/class/corlib/System/Array.cs:936)
System.Collections.ArrayList.CopyTo
(System.Array array) (at
/Applications/buildAgent/work/b59ae78cff80e584/mcs/class/corlib/System.Collections/ArrayList.cs:3053)
System.Collections.ArrayList.ToArray
(System.Type type) (at
/Applications/buildAgent/work/b59ae78cff80e584/mcs/class/corlib/System.Collections/ArrayList.cs:3219)
UnityScript.Lang.Array.ToBuiltin
(System.Type type)
deformation.FixedUpdate () (at
Assets/Scripts
Javascript/deformation.js:92)

I have not, of course, included all the code here, but could anyone, please, point me to the right direction?

What I actually want to achieve, is to collect the MeshFilters from the MeshColliders that a Raycast hits, and then retrieve them from another script.

I would like to provide it as an answer, since it solves my problem. The credits go to Eric, who helped me sort it out. So the lines I used:

I created a Public Variable to store my List:

var unappliedMeshes = new List.<MeshFilter>();

I added items:

unappliedMeshes.Add(unappliedMesh); //'unappliedMesh" is a MeshFilter retrieved from a mesh object.

And finally from another script, I call it as usually:

var theScriptWithMyList : ListScript; // supposedly that the script, in which the List is created, is called ListScript.
var meshFilters = theScriptWithMyList.unappliedMeshes
Debug.Log (meshFilters[3]);

Hi

My js knowledge is not the best since I use C# but lets try to help out.

var unappliedMeshes = new Array(MeshFilter);

with this you try to create an array that has the MeshFilter class as an object at index 0.

if you know how many unappliedMeshes you will have (say 10) you can replace the line with

var unappliedMeshes = new Array(10);

and then add MeshFilters with the Array’s Push()-Method.

if you want to init the array with an existing mesh filter do

var unappliedMeshes = new Array(unappliedMeshOne, unappliedMeshTwo);

Hope this helps and is what you are looking for.