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);
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.
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.
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.