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 *= step;*
 *step += 1.1;* 
*}*
*xVal = xArray[4];*
*```*
*<p>in the case above, Im thinking that xVal should be 3.3, but it errors</p>*
*<p>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.</p>*
*<p>Thanks a TON for the time!</p>*

2 Answers

2

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)

Should be fine yes, though as Jessy said, you should look into Generic lists for the long term

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.<float>();

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: http://answers.unity3d.com/questions/19642/cache-a-lot-of-object-transforms-in-a-builtin-array-iphone