Troble with converting strings to ints, again

I tried converting a string to an int using
System.Convert.ToInt32(text)

but it didn’t work, thanks.

using UnityEngine;
using TMPro;
using UnityEngine.UIElements;

public class Evasion : MonoBehaviour
{
    public TextMeshProUGUI Text;
    public GameObject[] dormant;
    public GameObject[] activated;
    public TextMeshProUGUI error;
    private string text;
    void Start()
    {
       
        error.GetComponent<TextMeshProUGUI>();
        foreach (GameObject gameObject in dormant)
        {
            gameObject.SetActive(true);
        }
    }
    // Update is called once per frame
    void Update()
    {

    }
    public void Calculation()
    {
        text=Text.text;
        int value = System.Convert.ToInt32(text);
        if (value<=0 || value>=101)
        {
            error.text="Invaild";
        }
        else
        {
            foreach(GameObject gameObject in dormant)
            {
                gameObject.SetActive(false);
            }
            foreach (GameObject gameObject in activated)
            {
                gameObject.SetActive(true);
            }
        }
    }
}

You have to supply more information other than "it didn't work"

–

1 Answer

1

You (I assume again) wrongly use the text value of the TextMeshProUGUI component. This component is NOT an input component. This is a visual representation component that is used by the input component. The text field may contain formatting characters.

The TMP_InputField is the component you want to look for.

If you’re not using an input field at all, then my question would be why you try to read data from a visual representation in the first place :slight_smile:

thanks, didnt know the difference!

–