How to Unity

Unity InputField: PIN Validation

Create, store and validate a PIN input.

Daniele Quero, PhD
4 min readMay 2, 2024

--

Create, store and validate a PIN input. How can we do that in Unity?

Using our knowledge about the Input Field in Unity, it’s a piece of cake. I set up a scene, with two Input Field objects set as PIN content and a UI text used as feedback. Only one Input Field will be active at a time.

The user will type a 4-digit PIN and submit it, if the PIN is valid it will be stored and the first InputField will disappear in favour of the second one, asking the user to enter the newly created PIN.

The text object will provide feedback on errors or successes.

A C# script will hold the callback method for the submission events. We’re going to use the Inspector this time.

using UnityEngine;
using UnityEngine.UI;

public class PinVerifier : MonoBehaviour
{
[SerializeField]
private Text _feedback; //Displayed text

[SerializeField]
private InputField _createPinInputField;

[SerializeField]
private InputField _enterPinInputField;

private string _pin; //stored PIN
private Color _errorColor = new Color(0.65f, 0.14f, 0.14f);//Dark green
private Color _okColor = Color.green;

public void CreatePin(string pin)
{
if(pin.Length < 4)//validate PIN…

--

--

Daniele Quero, PhD
Daniele Quero, PhD

Written by Daniele Quero, PhD

A professional developer with passion for game developing and skill-growing. A former Nuclear Physics Researcher who changed his life to pursue his dreams

No responses yet