- Home /
Implicit Downcast Warning and Converting Arrays
Hello Unit Answers community!
I'm having an issue with converting arrays. I'm trying to convert a js array to a builtin array like so:
 var arr : Array = new Array();
 var someBuiltinArray : float[];
 
 arr.Push(0.5);
 
 someBuiltinArray = arr.ToBuiltin(float);
When I do this, and put pragma strict in the script, I get a warning: "WARNING: Implicit downcast from 'System.Array' to 'float[]'." I think I know why this is happening (the function ToBuiltin is returning a js array instead of builtin array, and pragma strict points this out to me). I looked in the docs and this was how it was done, minus the #pragma strict part.
What I want to know is, is there a way how to do this without dynamic typing? I think there should there should be, but the answer eludes me. I know simply removing the pragma strict will make the warning go away, but that is missing the point.
Thanks for any and all responses! :D
Answer by Eric5h5 · Jan 04, 2013 at 07:17 PM
You should not ever use the Array class. For dynamically-sized arrays, use a generic List (should have "import System.Collections.Generic;").
 var myList = new List.<float>();
 myList.Add (0.5);
You can often just leave it at that, since List is much faster than Array and is type-safe, but if you need to convert to a built-in array, then do:
 someBuiltinArray = myList.ToArray();
Anyway, the warning about downcasting is just that, a warning. It's not an error. You can use #pragma downcast to suppress the warning.
Thanks, very helpful! I will try to replace all my non-builtin arrays to lists. Also useful to know what pragma downcast does, I've been having trouble finding the definitions and purpose of all the pragmas; they are not in the unity docs, but are only available in Unity (at least that's what google says xD). Thanks again!
There are 3 as far as I know: #pragma strict, which you already know, #pragma downcast, which prevents downcast warnings, and #pragma implicit, which allows implicit declaration of variables without using "var" (generally not a good idea to use that one; it's only there in case you need compatibility with non-#pragma strict code).
Answer by Setzer22 · Jan 04, 2013 at 11:22 AM
Hmm... I have to say I have no idea how the builtin arrays in Js work. But assuming you can access each sepparate element of that array and iterate through them, and know the size of the array (what a shame if an array can't do that, actually xD), you could use a very simple loop like this.
 //This is pseudocoded
 //Where I use arr.Length use whathever builtin method or property you have to check
 //the array's length
 
 for(int i = 0; i < arr.Length; ++i) {
     someBuiltinArray[i] = arr[i]
 
 }
I can see this working for now, but it would be good to know if there are other work-arounds. Thanks for the fast response, though! You've been very helpful. I wonder why "ToBuiltin" returns a js array, seems to be counterintuitive...
Your answer
 
 
             Follow this Question
Related Questions
Pragma strict and arrays 1 Answer
Implicit downcast and ArrayList 1 Answer
Pragma Strict - Implicit Downcast from 'Object' to 'UnityEngine.Transform' 5 Answers
Problem With Javascript to Built-in Array 0 Answers
I cant Get the value of an Array 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                