float.Parse changing dot position
Okay so i'm trying to convert 3 values (X, Y, Z) from a string into a vector 3, so that when i load a certain object it has that given coordinates. The problem is that when it load the object the X, Y and Z have a different "." position. For example the house I want to place had a position of: 4.4, 0.3, 5.0 but when the script places it, it has a position of: 44.0, 3.0, 50.0
The part of the script that converts from string to a Vector 3 looks like this:
public Vector3 StringToVector(string value)
{
value = value.Trim(new char[] {'(', ')', });
value = value.Replace(" ", "");
string[] pos = value.Split(',');
return new Vector3(float.Parse(pos[0]), float.Parse(pos[1]), float.Parse(pos[2]));
}
The string "value"is passed by another function, which contains the X, Y and Z positions. Can anyone help me? You might need to know that I took this script from a save and load tutorial, which explains how to save and load objects in the scene.
Thanks for your time!
Answer by Hellium · May 14, 2019 at 03:03 PM
I believe float.Parse
used the CultureInfo.CurrentCulture
to parse the number. If your current culture uses commas to separate decimals (French for instance), it produces this error. You will need to force the use of another culture info:
public Vector3 StringToVector(string value)
{
value = value.Trim(new char[] {'(', ')', });
value = value.Replace(" ", "");
string[] pos = value.Split(',');
System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
return new Vector3(float.Parse(pos[0], culture ), float.Parse(pos[1], culture ), float.Parse(pos[2], culture ));
}
Great, it worked!! I thought about doing what you just did, but i had no idea how to do it xD
Thank you :D
Your answer
Follow this Question
Related Questions
Check rotation for a Rigidbody2D 0 Answers
Reference Vector 3 variable to float variable. 2 Answers
How do I convert speed and heading to a vector? 3 Answers
Argument `#1' cannot convert `float' expression to type `UnityEngine.Vector3 ? 1 Answer
Non physics determinism of floating point math across platforms 1 Answer