Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by unity_szlhGXpz9gmI7Q · Apr 27, 2020 at 03:57 PM · scripting problemobjectfloatconvertconverting-var-type-to-other-var-type

Converting object to float in unity?

Hello ! I am following this tutorial : https://sites.psu.edu/bdssblog/2017/04/06/basic-data-visualization-in-unity-scatterplot-creation/

It is suppose to create plot from data. Simply reading x,y,z positions from csv file and display in 3d scene as point. And I faced obstacle. This function is used to convert from object to float :

float y = System.Convert.ToSingle( object );

And it doesnt work for all data. It seems to stop the code. I tried also converting first ToString(), float.Parse and TryParse. And it is all the same. I tried with some other csv file and it also wont work.

But when i simply take x,y,z as object and display it by Debug.Log, it displays every value fine. So it is not about the data but about converting it to float.

I stuck. Any ideas ? It really doesnt make sense:(

PS. I am using Unity 2019.3.9

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Apr 27, 2020 at 04:17 PM

There are many things unclear about your issue. First of all you said you used System.Convert.ToSingle( object ); However it's not clear what "object" is and what type it has. If the variable is indeed of type System.Object the question is what is actually is in the polymorphistic sense. ToSingle which takes an object does only work if that object implements the IConvertible interface. If the object is a string it would fail. However if the value you actually pass to the ToSingle method is of type string (in which case your code is completely misleading) you would actually use the overload of ToSingle which takes a string as argument then it might work.


So if we assume you actually used the string version of ToSingle the next question is what does the string actually contain? Note that ToSingle as well as float.Parse / TryParse do not tolerate wrong characters like spaces or newline characters to be passed in. Also, which is the most common issue, you should be aware of the culture you're using to parse the string representation. By default all those methods will use the current local culture. So what character is used as decimal point or thousand seperator might change depending on what's your local culture settings. For example I live in germany and we use the comma , as decimal point and the dot / period . as thousand seperator. It's the reverse for most of the english speaking world.


When using TryParse (which is in general the recommended way to read in unreliable user input) you can specify which culture you want to use when parsing the number. Apart from all the different culture settings for all the different countries there's also the invariant culture which isn't tied to any actual culture but defines a well standard which won't change ever. It's essentially the english culture for the most part. However should something change in the english culture in the future the invariant culture won't be affected.


Note that you might run into a similar issue with CSV in general. Here in germany the field seperator is the semicolon ;. So for example when you export / edit a CSV with MS Excel with a german culture setting the fields are actually seperated with a semicolon rather than a comma. So we don't know how you actually parse your CSV data but just make sure you correctly splitted your data and that you don't have any leading or trailing whitespace (spaces, tabs, newline, carriage return, ...) or other non printable control characters in your string.


If you need any further help with this issue we need to see:

  • How the actual incoming data looks like and how it's loaded

  • How you actually parse the CSV data into distinct fields

  • How you finally try to convert the values to a float.


Currently you just throw random terms at us without much information. This time I played the guessing game but that's not how questions here should be asked. You should provide clear and detailed information on your issue.

Comment
Add comment · 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

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

230 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Reference Vector 3 variable to float variable. 2 Answers

Cannot implicitly convert type 'UnityEngine.GameObjects[]' to 'float' 2 Answers

How to assign slider to float: Spin, on a gameobject 1 Answer

error CS0266: Cannot implicitly convert type `float' to `double'. An explicit conversion exists (are you missing a cast?) 0 Answers

I want Script To Move a object from a different object (In four Different directions,Randomly) . 0 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