Problems pushing GOs to an Array [SOLVED]

I’ve been successfully loading arrays by pushing things to a javascript array and then converting to a built-in for performance. But it doesn’t seem to work for GameObjects. If I try to push a game object to a js array then convert to a built-in, I get this error: ArrayTypeMismatchException: (Types: source=System.Object; target=UnityEngine.GameObject)

// This works:

var myArray : String[];
var dynamicArray : Array = new Array();

for (var i : int = 0; i < numberOfSlots; i ++) {
	daynamicArray.Push(SomeString);
}

myArray = dynamicArray.ToBuiltin(String);

// This does not:

var myArray : GameObject[];
var dynamicArray : Array = new Array();

for (var i : int = 0; i < numberOfSlots; i ++) {
	daynamicArray.Push(SomeGameObject);
}

myArray = dynamicArray.ToBuiltin(GameObject);

ArrayTypeMismatchException: (Types: source=System.Object; target=UnityEngine.GameObject)

Why is that? And is there a work around?

Works fine when I do this:

var myArray : GameObject[]; 
var objectToAdd : GameObject;
var dynamicArray : Array = new Array(); 

for (item in myArray) { 
   dynamicArray.Push(item); 
} 

dynamicArray.Push(objectToAdd);

myArray = dynamicArray.ToBuiltin(GameObject);

From the error message you’re getting, it sounds like you’re trying to add an System.Object rather than a GameObject. Also, you misspelled dynamicArray.

Spelling? I was rushing out that example… :slight_smile:

I’m not intentionally trying to push a system object, and what I’m pushing should be typed as : GameObject - that’s what’s confusing me. I’ll comb thru the code one more time and see if I’ve been a bozo somewhere.

Good to know it works for someone…

Sorry, I wasn’t trying to be the grammar police, I just thought maybe that was causing your problem!

I was a bozo…

But the fact that it worked for you kept me looking until I found it. Unfortunately this style doesn’t type that dynamic array, and something was not typed properly going into it, so it broke coming out.

Thanks for your help!