Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Matt 15 · Nov 15, 2010 at 01:04 AM · errorarrayrandomfloatconvert

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Matt 15 · Nov 15, 2010 at 01:18 AM 0
Share

will that be O$$anonymous$$ later, I'm working this with iOS?

avatar image Mike 3 · Nov 15, 2010 at 02:04 AM 0
Share

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

avatar image
1

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mike 3 · Nov 15, 2010 at 02:05 AM 0
Share

Same way as c#except you add a dot before the < for the generic. e.g. var test = new List.();

avatar image Jessy · Nov 15, 2010 at 04:59 AM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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

Array error, cannot convert float to float[] ???? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges