Sometimes, a password cannot be shorter than a lower bound character limit. This happens for a lot of reasons, including security. In Unity, we can easily set an upper bound limit for characters by specifying it in the Input Field (Script) component inside the Character Limit variable. In this recipe, what we are going to achieve is the checking of lower bound limits for characters in input fields. So, you will learn how to write a short script to handle this, and as a consequence, the input field will also be reset. This means an empty string is assigned to the text
variable.
using UnityEngine.UI;
statement at the beginning of the script. This is also because we are going to use the InputField
class. As we did in the previous recipe, we can add this line before the beginning of the class: [RequireComponent(typeof(InputField))]
(without the semicolon at the end). Thus, the script requires an InputField component attached to the same game object of it.private
variable to store the reference to our Input Field (Script) component. And we need a public
variable so that we can decide in the Inspector which is the lower bound limit. Therefore, let's write the following:private InputField inputField; public int characterLimit;
Start()
function, we are going to assign the Input Field (Script) component to our private
variable. We can get the reference by calling the GetComponent<InputField> ()
function. Thus, we can write the function in this way:void Start () { inputField = GetComponent<InputField> (); }
characterLimit
. If so, it empties the input field. Here is the function:public void Check (string newText) { if(newText.Length < characterLimit){ inputField.text = ""; } }
string
that the player has written to our script. Thus, we need an event to achieve this. In particular, if we look inside the Input Field (Script) component, we can see that there are two event tabs. Let's click on the small + sign on the End Edit (String) event tab in the bottom-right corner.characterLimit
.Like in the previous recipe, we have written a function that takes a string as a parameter. In fact, its role is to check whether the string length is less than characterLimit
, a variable that we can set in the Inspector. Then, in order to trigger this function, we created an event in the Input Field (Script) component. So, every time the player finishes typing, the check is performed, and if it fails, it means that the string is invalid. Then, the text of the input field is restored to an empty string. In fact, the event passes the string typed in the Input Field (Script) component to our function. It is possible to change the action that our function takes after the check (by changing our script), and we can also adapt it to our needs.
Giving feedback to the player is very important to let the player know what is happening and also to provide a response to his interaction within the game, albeit good or bad.
Even though there is a clear statement that reads this input field has a lower bound limit for the amount of characters that can be entered, which is well indicated to the player, it could still be confusing for the player if the text that he has just written disappears after he presses Enter.
One of the options that we have is giving an error message to the player. Since this recipe is similar to the previous one, you can refer to the There's more... section of the Making an input field with personal text validation at runtime recipe.
3.142.135.4