Hello,
I have a javascript/unityscript Array and i am using a float stored in the array and multiplying it.
I understand the array stores the float as an ‘object’, but how do i turn this back into a float?
I have tried:
var thearray : Array = new Array();
var number : float;
number = thearray[0];
number = number * 10;
… but it gives an error:
Operator ‘*’ cannot be used with a left hand side of type ‘Object’ and a right hand side of type ‘float’.
i have also tried using pragma downcast at the start, but it makes no difference
and i have tried the following:
var thearray : Array = new Array();
var number : float;
number = (float)thearray[0];
number = number * 10;
… which also doesn’t work.
Thanks for any answers you give, but please don’t just say ‘use a built in float array instead’ as i need some other features of the javascript array.
I spent half an hour trying to solve this, then i decided to post the question on Unity
Answers, only to find that 5 minutes later, i solved the problem myself.
If you want to know, i set the ‘object’ to a string:
var floatstring = “” + thearray[0];
… and got the float by using float.Parse:
number = float.Parse(floatstring);
It is not the ideal solution, and i expect there is a better one, but it works!
I had a similar problem. First I converted the object to a string and the string to float, but you are right, its not the ideal solution. The best way I found, is to not use the Javascript Array at all, but the Generic List class. There you can use float values without converting to string.
import System.Collections.Generic; //import
var theList = new ArrayList() = new List.(); //declaration
theList.Add(floatvalue); //add item to the array
The futures from the javascript array are supported but the syntax is a little bit different
http://wiki.unity3d.com/index.php/Choosing_the_right_collection_type
@the_genius