-1

I'm trying to validate a TextMeshPro form in Unity, but accessing the fields programmatically seems to be disabling them for some reason.

Here's what I have in my UI Manager script:

using UnityEngine;
using TMPro;


public class UserInputUI : MonoBehaviour
{
    private GameSceneManager gameSceneManager;

    [SerializeField]
    private TMP_InputField nicknameField;
    [SerializeField]
    private TMP_Dropdown suburbField;
    [SerializeField]
    private TMP_Dropdown ageField;


    private void Start()
    {
        gameSceneManager = GameObject.Find("SceneManager").GetComponent<GameSceneManager>();
        if (gameSceneManager == null)
        {
            Debug.LogError("GameSceneManager not found");
        }
    }


    private bool ValidateForm()
    {
        bool nicknameValid = false;
        bool suburbValid = false;
        bool ageValid = false;

        if (string.IsNullOrEmpty(nicknameField.text) == false)
        {
            nicknameValid = true;
        }
        if (suburbField.value == 0)
        {
            nicknameValid = true;
        }
        if (ageField.value == 0)
        {
            nicknameValid = true;
        }

        if (nicknameValid && suburbValid && ageValid)
        {
            return true;
        }
        Debug.LogError("You must fill out all fields");
        return false;
    }


    public void SubmitForm()
    {
        if (ValidateForm())
        {
            gameSceneManager.ChangeScene(2);
        }
    }
}

I've attached the script to the Canvas. If I leave any of the serialised fields unpopulated in the inspector, then that field works as expected. As soon as I drag the reference in, the field stops working - no response to mouse clicks so I can neither type in the text field or open the dropdown.

Has anyone ever seen anything like this before? I've searched around, but I can't find anything that helps.

Seona
  • 43
  • 1
  • 7
  • Not sure. This looks weird, bc value is a string. suburbField.value == 0 – mrVentures May 16 '23 at 21:25
  • How is SubmitForm called? – mrVentures May 16 '23 at 21:26
  • @mrVentures Value on a TMPDropdown field is actually the index of the selected option, and so is an int. The final validation will actually be "if != 0" but I haven't populated the dropdowns with data yet so they only have the default value. – Seona May 17 '23 at 01:13
  • SubmitForm is called on a button click. – Seona May 17 '23 at 01:14
  • Hmm Im not sure. This script is small, I would suggest sanity checks. Comment out the code until it works, then work your way backwards. – mrVentures May 17 '23 at 01:19

1 Answers1

0

-sigh- I've found the problem. PEBKAC.

If I try and submit the form when it doesn't pass the validation, then it throws an error. Unity then helpfully pauses the play window. This means that nothing works no matter how many times I click it.

This is why you shouldn't code while tired, kids!

Seona
  • 43
  • 1
  • 7