I have a problem where, when try and convert a TMPro.text into an integer with the Parse function is says “FormatException: Input string was not in a correct format.”. I tried the same function with a string variable set to “10” and that worked completely fine, I also used the get getType function which said that the TMPro.text was a string. I have already searched a lot of forums but none of them seemed to have my answer. This is basically what my code looks like
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class Player : MonoBehaviour
{
public int speed;
public float jumpHieght;
public Vector2 movement;
public Rigidbody2D rb;
public SpriteRenderer sr;
public TextMeshProUGUI speedInput;
public string stringSpeed;
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = 0;
if (movement.x == 1) {
sr.flipX = false;
}
if (movement.x == -1) {
sr.flipX = true;
}
if (Input.GetKeyDown(KeyCode.Space)) {
rb.AddForce(new Vector3(0, jumpHieght * 100, 0));
}
}
void FixedUpdate()
{
// Movement
rb.velocity = new Vector2(movement.x * (speed * 10) * Time.fixedDeltaTime, rb.velocity.y);
}
public void ReadFloatInput() {
stringSpeed = speedInput.text;
//stringSpeed = "50";
Debug.Log(stringSpeed);
speed = int.Parse(stringSpeed);
}
If anyone can figure out why it isn’t working this isn’t working that would be super helpful, thanks