- Home /
JS Array Object to Float
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.
Answer by the_genius · Sep 15, 2014 at 08:44 PM
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!