Built a Multi-Digit PIN Pad in Unity

Tyler Smallwood
4 min readSep 20, 2023

--

Objective: Build a pin pad and check to see if the pin is correct!

Using buttons and some a little bit of coding, we can get a working pin system. To start we will need 12 buttons(if you want to use all digits) numbered 0–9.

Next we can create an image and leave it as default sprite and size it how you would like. Then you can create a text, size it to the image block and then set as a child to the image.

Now we will create two scripts, one for the buttons and one for the pin number.

In the button script we will create two variable, one for the button value and one for the pin number script.

Back in Unity, select all the number buttons and drop the button script onto them.

Go through all the number buttons and set the button value to the button.

In the PinNumber script, grab the TMPro library. Create variables for the text block the pin number will go into, the secret pin which would be the pass code, and finally the actual pin that the user inputs.

Now we will create the method to enter the pin based off the number and display it. Create a public method that takes in an int. Next we will check to see if the actual pin length is greater than or equal to 5( this can be more if you like.). Then we will set the textblock to clear out and use the += operand and add in the pin values by setting the int to a string. Last thing to do is set the textblock to equal the actualpin.

Back in the ButtonScript, we will create another public method and call the PinNumber’s public script and pass in the ButtonValue.

Before we go back to Unity, let’s create two more public scripts for the submit and clear buttons. Back in PinNumber script, we will create another public method for Submitting the Pin. In here we will check to see if the actual pin is equal to the secret pin. If it the same, let the user know. If not let them know. For both checks, set the actual pin back to nothing by setting it to “”;.

For Clearing the pin we will create yet another public method and in this method we will just set the actual pin to “”; again and set the textblock to equal the actual pin.

Back in Unity we will set up all the buttons. Select the numbered button and in the OnClick section add a new event and drop in button object that was selected. After wards select the EnterPin method from the button script.

With this we can get the numbers to appear in the pin pad.

Now select the Clear button and submit buttons. In their OnClick() section, add a new event and instead of dropping in the button script, you will drop in the object that has pin number pin. Select the respective method for each button.

Finishing this up completes the pin pad.

Don’t forget to test as you go! Thanks for reading! Have a wonderful day!

--

--

Tyler Smallwood
Tyler Smallwood

Written by Tyler Smallwood

I am passonate on learning to program and use Unity to become a skillful Unity Developer

No responses yet