Hello !
I am following this tutorial : Beginning Data Visualization in Unity: Scatterplot Creation – Methodology blog
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
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.