Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Inputfield return key and move on

Discussion in 'Scripting' started by hrohibil, Aug 26, 2021.

  1. hrohibil

    hrohibil

    Joined:
    Apr 17, 2021
    Posts:
    267
    Hello

    I have a inputfield and a button below.

    inputfield is for player name and that works great.
    When pressing enter the name gets stored but remains on same page. that’s why I created a button to go to next page.

    I want to remove the button so when user enters the name and click enter/return then move to next page.
    How do I make so the name has to be filled or else you can’t move to next page..
    thanks
     
  2. diXime

    diXime

    Joined:
    Oct 2, 2018
    Posts:
    162
    Hello,
    That is some basic "form" verifications. Usually, incredibly good programmers will use regular expressions, to check if it's a proper e-mail and so on.
    In your case, you can simply make an "if" statement, checking if the input field is empty or made only of white spaces. Depends on your architecture, but you can even put it in Update :
    Code (CSharp):
    1. string txt = textComponent.text; // to replace with your own component
    2. txt = txt.Replace(" ", System.String.Empty); // to remove any spaces
    3.  
    4. if (txt == "") // check if empty once we removed the white spaces
    5.         button.enabled = false;
    6. else
    7.         button.enabled = true;
    8. // enables or disables the button if conditions are met
    9.  
    Don't forget to null check and so on.
     
    Last edited: Aug 26, 2021
    hrohibil likes this.
  3. hrohibil

    hrohibil

    Joined:
    Apr 17, 2021
    Posts:
    267
    I don't need the button.
    Just a input field, when user enter some text and press enter, they move on but the field canot be empty..
    If that makes since..

    Is there not a way with the input field visual options?
     
  4. diXime

    diXime

    Joined:
    Oct 2, 2018
    Posts:
    162
    I should have read better, my bad.
    Instead of activating / deactivating a button, you can check for Input.GetKeyDown.
    Code (CSharp):
    1. if(Input.GetKeyDown(KeyCode.KeypadEnter))
    2. {
    3. if(txt == "")
    4.   MoveOn();
    5. }
    Maybe you can check for values standarts with Content Type in Input Field Settings if you're using TMPro. But I'm not sure you can check a form and perform those actions without a little bit of scripting. If you're really allergic to c#, maybe check with UIToolkit but being in the "scripting" section I have assumed you wanted some piece of code ;)

    you can put MoveOn() into the OnEndEdit section, but beware, it would also move on when using tab and other keys.
     
  5. hrohibil

    hrohibil

    Joined:
    Apr 17, 2021
    Posts:
    267
    Ok I will go the script way, but right now I just have a UI inputfield. On that i have 2 scripts attached to the "On End edit".
    They hold the create user name and store it and then the other script is a public void move to next panel.
    So do i create a new script and attach it to the inputield??

    Below is code to store the the username. It shoud be in here somewhere?

    Code (CSharp):
    1. public void CreateUserName()
    2.     {
    3.         //Create a save method like a button to store the nane
    4.         playerNameText.text = playerInputField.text;
    5.         PlayerPrefs.SetString("user_name", playerNameText.text);
    6.         PlayerPrefs.Save();
    7.  
    8.     }
     
  6. hrohibil

    hrohibil

    Joined:
    Apr 17, 2021
    Posts:
    267
    I tried to due it with bools.

    SO that if something has been entered it sets it to true and then can move on.
    But now after i enter a name or anything and press enter, nothing happens..

    Code (CSharp):
    1.  public void CreateUserName()
    2.     {
    3.         if (Input.GetKeyDown(KeyCode.KeypadEnter))
    4.         {
    5.             if (playerInputField.text == "")
    6.                 storeName();
    7.             userNameEntered = true;
    8.  
    9.         }
    10.  
    11.      
    12.  
    13.     }
    14.  
    15.  
    16.     public void storeName()
    17.     {
    18.  
    19.  
    20.        
    21.         //Create a save method like a button to store the nane
    22.         playerNameText.text = playerInputField.text;
    23.         PlayerPrefs.SetString("user_name", playerNameText.text);
    24.         PlayerPrefs.Save();
    25.  
    26.  
    27.     }
    28.  
    And here is the code for the panel to be open if the condition is true

    Code (CSharp):
    1.  public void introThree()
    2.     {
    3.  
    4.         if(gs.userNameEntered == true)
    5.         {
    6.  
    7.             panelIntro3.SetActive(true);
    8.             panelIntro.SetActive(false);
    9.             panelStart.SetActive(false);
    10.             Time.timeScale = 0f;
    11.             GameIsPaused = true;
    12.  
    13.         }
     
  7. hrohibil

    hrohibil

    Joined:
    Apr 17, 2021
    Posts:
    267
    Ok..
    Thanks ..
    I got it now :)
    That NULL saved med...