C#C
C#3y 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;
    }
Was this page helpful?