C
C#2y ago
zandose

❔ I'm trying to convert a Vector3 string to a Vector3 but the numbers returned aren't correct.

// Converts a string to a Vector3.
// Examples: 's' can be ".5,.5,.5" or "1".
public Vector3 ConvertStringToVector3(string s)
{

string StringToConvert = s;
StringToConvert.Split(',');
StringToConvert.Select(part => float.Parse(StringToConvert));
StringToConvert.ToArray();

Debug.Log("xyz = s::" + StringToConvert);
Debug.Log("xyz.Split(',')::" + StringToConvert); // Returns .5
Debug.Log("xyz.Select(part => float.Parse(xyz))::" + StringToConvert); // Returns .5
Debug.Log("xyz.ToArray()::" + StringToConvert); // Returns .5

Vector3 vector3;

if (StringToConvert.Length == 1) // If the string 's' is only using one number instead of three, like "1".
{
//float[] xyzf = new float[] { float.Parse(xyz[0], System.Globalization.CultureInfo.InvariantCulture) };
Debug.Log("xyz[0]::" + StringToConvert[0]);
vector3 = new Vector3(StringToConvert[0], StringToConvert[0], StringToConvert[0]);
Debug.Log("vector3::" + vector3); // The number that comes out is not "1"
}
else // If the string 's' is three numbers, like ".5,.5,.5".
{
Debug.Log("xyz[0]::" + StringToConvert[0]);
Debug.Log("xyz[1]::" + StringToConvert[1]);
Debug.Log("xyz[2]::" + StringToConvert[2]);
vector3 = new Vector3(StringToConvert[0], StringToConvert[1], StringToConvert[2]);
Debug.Log("vector3::" + vector3); // The number that comes out is not ".5"
}

return vector3;
}
// Converts a string to a Vector3.
// Examples: 's' can be ".5,.5,.5" or "1".
public Vector3 ConvertStringToVector3(string s)
{

string StringToConvert = s;
StringToConvert.Split(',');
StringToConvert.Select(part => float.Parse(StringToConvert));
StringToConvert.ToArray();

Debug.Log("xyz = s::" + StringToConvert);
Debug.Log("xyz.Split(',')::" + StringToConvert); // Returns .5
Debug.Log("xyz.Select(part => float.Parse(xyz))::" + StringToConvert); // Returns .5
Debug.Log("xyz.ToArray()::" + StringToConvert); // Returns .5

Vector3 vector3;

if (StringToConvert.Length == 1) // If the string 's' is only using one number instead of three, like "1".
{
//float[] xyzf = new float[] { float.Parse(xyz[0], System.Globalization.CultureInfo.InvariantCulture) };
Debug.Log("xyz[0]::" + StringToConvert[0]);
vector3 = new Vector3(StringToConvert[0], StringToConvert[0], StringToConvert[0]);
Debug.Log("vector3::" + vector3); // The number that comes out is not "1"
}
else // If the string 's' is three numbers, like ".5,.5,.5".
{
Debug.Log("xyz[0]::" + StringToConvert[0]);
Debug.Log("xyz[1]::" + StringToConvert[1]);
Debug.Log("xyz[2]::" + StringToConvert[2]);
vector3 = new Vector3(StringToConvert[0], StringToConvert[1], StringToConvert[2]);
Debug.Log("vector3::" + vector3); // The number that comes out is not ".5"
}

return vector3;
}
8 Replies
zandose
zandoseOP2y ago
For instance, I'll have the string "1" and everything appears to be fine with the debug messages but when it's used in the actual vector3 = new Vector3(StringToConvert[0], StringToConvert[0], StringToConvert[0]); it changes to the numbers 49.
hiyosilver
hiyosilver2y ago
49 is the ascii code for the character '1'. You can't use it directly as the number it represents. You know it's a 1, but your computer doesn't treat it that way. I see you are already using Parse but then you use the chars from the original string to construct your vector which won't work.
.logik.
.logik.2y ago
The string methods you're using do not affect StringToConvert, they return an object which you're not saving to a variable. Currently what vector3 = new Vector3(StringToConvert[0], StringToConvert[1], StringToConvert[2]); is doing is vector3 = new Vector3('0', '.', '5'); which should likely be evident in your debug logs. For example, StringToConvert.Split(','); will return you a string array which needs to be saved to a string[] variable. Alternatively just chain the methods and save it into a float[] variable. Also your LINQ is going to throw an error because you need to be parsing part as opposed to the initial string.
circles.png
circles.png2y ago
linq methods have no side effects so they only return the result
zandose
zandoseOP2y ago
Thank you all for your help in understanding the issue. I believe I have found a solution by using
public Vector3 ConvertStringToVector3(string s)
{
float[] f = s.Split(',').Select(part => float.Parse(part)).ToArray();
Vector3 vector3;
if (f.Length <= 2) { vector3 = new Vector3(f[0], f[0], f[0]); }
else { vector3 = new Vector3(f[0], f[1], f[2]); }
return vector3;
}
public Vector3 ConvertStringToVector3(string s)
{
float[] f = s.Split(',').Select(part => float.Parse(part)).ToArray();
Vector3 vector3;
if (f.Length <= 2) { vector3 = new Vector3(f[0], f[0], f[0]); }
else { vector3 = new Vector3(f[0], f[1], f[2]); }
return vector3;
}
.logik.
.logik.2y ago
looks good to me, don't forget to $close if there are no more questions
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server