Unity3D Keypad Door unlock script c# UI System

Hey guys!

i’m working on a VR game at the moment, and since I am learning c#,  I realised that I should share my new UI Keypad script with you. This is usful for your game if you have a door with a keypad UI.

The asset that I use for the keypad textures can be downloaded for free here

So, attach this script to any object, and then drag in your UI buttons into the variables in the inspector, then also the code you want.  Here is the script-

//Keypad.cs-

//Created by Christian Blandford
using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Keypad : MonoBehaviour {
 
 
     public string curPassword = "12345";
     public string input;
  
     public Button ButtonOne;
     public Button ButtonTwo;
     public Button ButtonThree;
     public Button ButtonFour;
     public Button ButtonFive;
     public Button ButtonSix;
     public Button ButtonSeven;
     public Button ButtonEight;
     public Button ButtonNine;
     public Button ButtonZero;
     public Button clearButton;
     public Button goButton;
 
     void Start()
     {
         ButtonOne.GetComponent<Button>().onClick.AddListener(PressOne);
         ButtonTwo.GetComponent<Button>().onClick.AddListener(PressTwo);
         ButtonThree.GetComponent<Button>().onClick.AddListener(PressThree);
         ButtonFour.GetComponent<Button>().onClick.AddListener(PressFour);
         ButtonFive.GetComponent<Button>().onClick.AddListener(PressFive);
         ButtonSix.GetComponent<Button>().onClick.AddListener(PressSix);
         ButtonSeven.GetComponent<Button>().onClick.AddListener(PressSeven);
         ButtonEight.GetComponent<Button>().onClick.AddListener(PressEight);
         ButtonNine.GetComponent<Button>().onClick.AddListener(PressNine);
         ButtonZero.GetComponent<Button>().onClick.AddListener(PressZero);
         clearButton.GetComponent<Button>().onClick.AddListener(PressClear);
         goButton.GetComponent<Button>().onClick.AddListener(PressGo);
     }
 
     void PressOne() //Press Button One on Keypad
     {
         input = input + "1"; 
     }
     
     void PressTwo() //Press Button Two on Keypad
     {
         input = input + "2";
     }
     
     void PressThree() //Press Button Three on Keypad
     {
         input = input + "3";
     }
     
     void PressFour() //Press Button Four on Keypad
     {
         input = input + "4";
     }
     
     void PressFive() //Press Button Five on Keypad
     {
         input = input + "5";
     }
     void PressSix() //Press Button Six on Keypad
     {
         input = input + "6";
     }
     void PressSeven() //Press Button Seven on Keypad
     {
         input = input + "7";
     }
     void PressEight() //Press Button Eight on Keypad
     {
         input = input + "8";
     }
     void PressNine() //Press Button Nine on Keypad
     {
         input = input + "9";
     }
     void PressZero() //Press Button Zero on Keypad
     {
         input = input + "0"; 
     }
     void PressClear() // When you press clear 
     {
         input = ""; // reset the input string to empty
     }
     void PressGo()        //Press the GreenButton(GoButton)
     {
         if(input == curPassword)        // if it is right 
         {
             Debug.Log("CorrectPassword!!!");
         }
         else {
                 Debug.Log("Wrong :(");
         }
     }
 } 

//I hope this helps :)

Leave a Reply

Your email address will not be published. Required fields are marked *