String can't be converted to float or int?

Hello,

Im working on a small side project for fun right now, but with this issue, it isnt fun anymore.
I want to create a console where you can program simple commands like Rotate [angle] and Move.


The problem is, i cant convert my extracted argument from the console to an intager or float.

I’ve tried everything from Parsing to Converting, Casting and i even wrote an loop that loops through all possible values and compares them to the string if one is the right number and returns it, but it just dosnt work with no real reason

i have been reading a lot through other answers that explain i need some Culture info or something but that didn’t work either.
.

Player movement:

public class PlayerController : MonoBehaviour
{
    public Animator anim;
    public float playerMoveStep = 1.2f;

    private void Awake()
    {
        GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;

        print("Timer Started..");
        StartCoroutine("timer");
    }

    public void Move()
    {
        transform.position += transform.forward * playerMoveStep;
        anim.Play("Player_MOVE");
    }

    public void RotatePlayer(string sAngle)
    {
        int angle = int.Parse(sAngle);
        transform.rotation = Quaternion.Euler(0, angle, 0);
    }

    IEnumerator timer()
    {
        yield return new WaitForSeconds(1f);
        GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
        GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
        print("Timer ended");
    }

}

Console System:

public class CommandConsole : MonoBehaviour
{
    public TMP_InputField console;

    public string command;
    public string args;

    public void RunCode()
    {
        List<string> lines = new List<string>();
        lines = EnumerateLines(console.textComponent);

        StartCoroutine("RunLines", lines);
    }

    List<string> EnumerateLines(TMP_Text text)
    {
        TMP_TextInfo textInfo = text.GetTextInfo(text.text);
        List<string> lines = new List<string>();

        for(int i = 0; i < textInfo.lineCount; i++)
        {
            TMP_LineInfo line = textInfo.lineInfo*;*

lines.Add(text.text.Substring(line.firstCharacterIndex, line.characterCount));
}
return lines;
}

void MovePlayer()
{
PlayerController player = GameObject.FindObjectOfType();
player.GetComponent().Move();
print(“Moving player…”);
}

void RotatePlayer(string angle)
{
PlayerController player = GameObject.FindObjectOfType();
player.GetComponent().RotatePlayer(angle);
print(“Rotating player…”);
}

IEnumerator RunLines(List lines)
{

for (int i = 0; i < lines.Count; i++)
{
string _command = lines*;
_command = _command.Split(’ ')[0];*

command = _command;

if (_command.Contains(“Move”))
{
Debug.Log(_command);
MovePlayer();
}

if (_command.Contains(“Rotate”))
{
args = lines*.Split(’ ')[1];*
Debug.Log(_command);
RotatePlayer(lines*.Split(’ ')[1].ToString());*
}

yield return new WaitForSeconds(0.5f);
}
}
}

I would really appreachiate it, if you could answer this question because i am really struggeling with it.
(I even copied some code from another project where this float parsing works and even that didnt work)

Well we can’t really do much on our side. Have you actually checked if you have any additional characters in the string you pass to int.Parse? Any non digit character would throw the parser off. This could also be any non visible characters like a leading or trailing space, tab or newline character.

So the first thing you should check is what is actually passed to your “RotatePlayer” method. Try adding a Debug.Log statement like this to your method:

Debug.Log("RotatePlayer: >"+sAngle+"< ("+sAngle.Length+")");

See if the character count matches the actual characters you expect. Another thing one could do is disecting the string into ascii bytes or utf8 bytes and printing out those to see if there’s anything odd.

Another thing that a lot people forget that int.Parse and float.Parse by default use the local culture settings which may throw things off depending on the local culture settings. While this is usually much more important for floating point numbers, it may even have an effect on integers.