Built a Multi-Digit PIN Pad in Unity.

Suleiman Abdullah
4 min readAug 29, 2023

--

Objective: Built a Multi-Digit PIN Pad in Unity.

Before doing this add a background of your choice.

In the first step create a button and set its width and height to 30 by 30. Set the text to No 1. Then I will manipulate some of the color option

Duplicate the above button and rename it to 2 and change the text content of it, then duplicate those two and rename them to clear and submit, position them below button no.

Note: I will uncheck the best fit and reduce font size, so I can set the text to what I think fits well.

Create a new Script called ButtonNumber and attach it to the Button_1 and Button_2.

Create an Image place it to the top, then add a text component.

Note: When creating this text dont create inside the image directly it will stretch the text.

Create a script called PinManager, and create 3 variables, 2 strings called _secretPin,_actualPin, and a text called _textBox, Then Create an empty game object called PinManager and attach this script to it. Initialize the _secretPin to whatever text you like, make sure you remember it when testing the system.

Open the ButtonNumber script, create two variables, and int called _buttonValue, a PinManager called _pinManager. Select the buttons set the value according to the button displayed text, and then drag the PinManager object to the field in the inspector.

Create a public method in the ButtonNumber script, called PassButtonValue.

Open PinManager script creates a public method called AddDigitToUI which passes a string.

In the AddDigitToUI, we need to clear _textBox first, we do this by setting textBox to empty using an empty string. Then we need to increment _actualPin by buttonNumber which is the parameter of this method, last we need to display the textBox with _actualNumber.

Then we need to pass the buttonValue of the pressed Button to buttonNumber parameter of AddDigitToUI, We do this by calling the AddDigitToUI to PassButtonValue method, then we pass _buttonValue as the parameter of AddDigitToUI.

Note: Since _buttonValue is an integer, we need to convert it to a string using the ToString method.

Add Event to the 2 buttons with numbers, then pass the same button as an object in the event, and call the PassButtonValue method.

Create a public method called Submit, first, we check if _actual pin is equal to _secretPin, then clear the _textBox then set _actualPin to null last we need to tell the user he is successful. And if the condition is never met we clear the text box and set _actualPin to null and prompt a message to the User.

Create a public method called Clear, then clear the _textBox and set _actualPin to null.

Note: We are setting the _actualPin to null because if we did not set it null the second time we write a pin we will add the previous pin to the currently entered pin which is not correct logic.

No Add event to Button_Clear and Submit, then PinManager Object last, call submit and clear method.

Select PinManager Object then drag and drop the Text to inspector.

Go to the PinManager script in the start method and set the textbox to empty.

This is how to create a Multi-Digit PIN Pad in Unity3d.

See you in the next one.

--

--

Suleiman Abdullah
Suleiman Abdullah

Written by Suleiman Abdullah

Self taught Unity Developer, who is passion about making games ,and he loves Math For Game Development

No responses yet