Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

I am trying to take an integer value out of an input field

tuppedtupped Member
edited May 23 in Programming

But whenever I hit play in unity, this error shows up :

FormatException: Input string was not in a correct format.

System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) (at <437ba245d8404784b9fbab9b439ac908>:0)

System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) (at <437ba245d8404784b9fbab9b439ac908>:0)

System.Int32.Parse (System.String s) (at <437ba245d8404784b9fbab9b439ac908>:0)

ButtonManager.Update () (at Assets/Scripts/Button

Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;


public class ButtonManager : MonoBehaviour
{


    public TMP_InputField input;
    public GameObject playerCharecter;


    int InputNumber;


     private void Update()
    {


        InputNumber = int.Parse(input.text);
        playerCharecter.GetComponent<PlayerCharecter>().extraJumpsValue = InputNumber;
        Debug.Log(InputNumber);
    }


Answers

  • Roby65Roby65 Member

    You are doing this at every update, even when the input is empty! You should do this when clicking on a button, and you should use TryParse to avoid the exception:

    if (int.TryParse(w.text, out InputNumber))
    {
       //InputNumber is correctly set!
    }
    else
    {
       //Not a number!
    }
    
Sign In or Register to comment.