- Home /
What array-type does GetComponents return?
Hi.
in my code I have:
speedParameters = GetComponents."tag" ParameterSpeed "tag"();
I'm using the generic "GetComponents" function and it returns a valid array.
What I want to know is:
What type of array does GetComponents return (by "type" I mean built-in or normal)? The documentation doesn't state this.
more generally: how can I check what type of array an array is?
If the array isn't built-in, how can I specificly convert it into a built-in one to improve performance?
Answer by aldonaletto · Feb 12, 2013 at 03:44 PM
As far as I know, Unity functions always return built-in arrays - any other array type would be more expensive. Anyway, you can use .GetType() to get a string describing the complete type: built-in arrays return things like "System.Int32[]", while a List returns "System.Collections.Generic.List..." - for instance:
var myArray: int[] = new int[100];
print("myArray type = "+mayArray.GetType());
You can convert from Unityscript Array class (veeery slow and problematic) to builtin with ToBuiltin:
var builtinArray : Vector3[] = array.ToBuiltin(Vector3);
So if I declare the speedParameters-array from the above example like this var speedParameters:ParameterSpeed[];
it should be a builtin? Using .GetType() returns type "ParameterSpeed[]". So I gather all arrays declared with square-brackets are builtin arrays?
Yes, built-in is the standard array type - every array you declare with the square brackets is built-in. You have to explicitly import the namespace (like import System.Generic.Collections;) in order to use other array types. The exception is the Unity Array class, but it's better to never use it: if you need a non-fixed size array, import the namespace like above and use a List ins$$anonymous$$d - List is an efficient typed array, much safer and faster than the Array class.
Thanks for clearing that up! This helped me a lot! Cheers!
Not sure if this is a silly inquiry: How to declare the type of a builtin array to have the same type as a generic list of which the type is unknown? e.g. Given a genericList of unknown type something like: var listType = genericList.GetType(); var builtin = new listType[genericList.Count];
Answer by Jake-L · Feb 12, 2013 at 03:19 PM
Hi,
GetComponent returns a builtin array as you can see from the syntax (Component*[]*)
[] are builtin arrays, Array to Javascript arrays.
Please see here: http://docs.unity3d.com/Documentation/ScriptReference/Array.html
Hope that helps