- Home /
Call array-specific methods on arrays within an array?
I have a function that requires input in the form of an array, but I want to be able to specify which array is passed to the function. I have initialized several arrays (builtin, if that's relevant) and stored them inside another array (JS Array).
Here is a simplified example of what I've got:
var numbers = new String[3];
numbers[0] = "1";
numbers[1] = "2";
numbers[2] = "3";
var letters = new String[3];
letters[0] = "1";
letters[1] = "2";
letters[2] = "3";
var names : Array = new Array(
numbers,
letters
);
function Evaluate(input) {
for (var each in input) {
print (each);
}
}
function Start () {
Evaluate(names[1]);
print(names[0].Length);
}
When I run the script, the Evaluate function works as expected. However, the print statement throws the following error: "Assets/NewBehaviourScript.js(27,24): BCE0019: 'Length' is not a member of 'Object'."
I'm guessing that I have to let Unity know that the contents of array "names" are all of type "builtin array", and that they DO support to .Length method. I've gotta supply the type somehow. Where? How?
Any explanation would be very helpful, because I've been trying to figure this out forever... with absolutely no success.
Good thought! Unfortunately, I had already tried both capitalizations, receiving the same result regardless. ($$anonymous$$ore detailed comment below) Any other possibilities?
Answer by Loius · Mar 25, 2013 at 03:29 AM
Array is a terrible class. Don't use it unless you really really gotta have that type indifference.
Array can only store Object references. Any time you want to use an element of an Array, you need to cast it: var interiorArray : Array = exteriorArray[0] as Array
array[0].Length
doesn't work because array[0] is an Object, not an Array.
Use List to get around every problem with Array. It has all the same functionality and more besides, and it operates quite a bit faster and you never need to cast out of it. The only thing you need to do to change from Array to List is:
var myArray : Array = new Array();
becomes
var myList : List.< Type > = new List.< Type >();
Actually you shouldn't use ArrayList either, it's not really any better than Array and has the same issues. I assume you mean List? (Also, there's really no reason to use Array ever...if you need an untyped array you can make a List of Object and it will still be faster/better than Array.)
Yes, I know, Array is horrible. However, I couldn't use any other collection, because I couldn't figure out how to specify the "type" of a builtin array. How would I go about creating, say, a Generic List (or even a bulitin array) with elements of builtin type? What should I do to translate my code over to a better class?
The type of a builtin String array is String[]. I don't know how representative that code is, but it sounds maybe like you should make a custom class and then make an array or list using that class, rather than using parallel arrays.
Right, List, my bad. Too much awake, not enough sleep.
Your String[] arrays could be held in a List.< String[] >
Now we need to find a way to get { } on the same line and we can have all the parentheticals going on.
if ( true ) { var = new List.< String[] >(); }
owch, my eyes
Your answer
Follow this Question
Related Questions
my2dArray.Length(0) 2 Answers
How do I get unity to tell me what type of variable a variable is? 1 Answer
Sorting array 0 Answers
Converting Builtin Array to Generic List 2 Answers