- Home /
convert object to float
K, I feel like an absolute noob right now.
My question is, i have an array that has float values in it, but when i try to assign one of those values to a variable (that is a float) it errors with:
Cannot convert 'Object' to 'float'
Did i make the array wrong? or is there a way to change the objects in my array into their float values?
private var xArray = new Array(15); private var step : float = 0.0; private var xVal : float = 0.0;
for(i = 0; i < xArray.length; i++){ xArray[i] = step; step += 1.1;
}
xVal = xArray[4];
in the case above, Im thinking that xVal should be 3.3, but it errors
what i need to do is create a list of numbers that follow this pattern (0.0, 1.1, 2.2, 3.3, 4.4) etc. up to 15.4.
I'm then going to get a random number (btw 0-15) that I plan on using as the index of this array to pull one of these numbers.... if there is a better way to do this, (and I'm sure there is_) I'm all ears.
Thanks a TON for the time!
Answer by Mike 3 · Nov 15, 2010 at 01:16 AM
Add #pragma downcast to the top of your script, that should fix it (it'll allow casting from a base type to a more specific type)
will that be O$$anonymous$$ later, I'm working this with iOS?
Should be fine yes, though as Jessy said, you should look into Generic lists for the long term
Answer by Jessy · Nov 15, 2010 at 01:41 AM
Using Generic Lists will solve your problem and make your code run faster; I believe they make the Array class obsolete. Unfortunately, like the rest of UnityScript, there's next to no information on how to use them. This is all I know of that I can offer you, unfortunately:
http://unity3d.com/support/documentation/Manual/MonoUpgradeDetails.html
Same way as c#except you add a dot before the < for the generic. e.g. var test = new List.();
That part's easy enough, but that's not complete information. I gave using them a shot, but only ran into errors without suggestions for fixes. If you feel comfortable with them, I suggest answering this question, so we can learn the proper syntax:
Your answer
Follow this Question
Related Questions
Convert type `float' to `bool', Load 1 Answer
Pick between two floats 2 Answers
Setting float variable to an Array value 1 Answer
Vector 3 to float eg V3(1, 2, 3) -> float 123 in JS? 1 Answer