Category Archives: Game advice

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 :)

First Post!

Hello! This is the first post on what I hope will be a regular blog! The aim to post updates on development of my games. At the moment am working on “Behind” a virtual reality game for the oculus rift. I will do a version for normal monitors too.

Okay, Today I worked on The “Bedroom Sequence”  and The “Dining Room Sequence”

You can see the videos on my Youtube Channel and Below-

Note to learning Developers-

The way I did the locked doors in my games is the following-

I have two Prefabs, one door that Opens and one door that is locked both in the same place, only One Active. ( e.g if I wanted the door to be locked I would Keep the locked door active and the Unlocked door Un-active. (If any one wants a post about how to make a door Open I will reply to you when I have time) Here is the Code I Made-

Attach to your Key Object-

*Make Sure you have A Box collider attached to the key Object, and check the “Is Trigger” and Make it big enough for the range :D*

///////////////////////////////////////////////

var InRange : boolean;  //Determines If your Character is near

var DoorUnlocked : GameObject;  // assign the Unlocked Door

var DoorLocked : GameObject; // assign The Locked Door

var ForCorrutine = false;  // random Shiz ( will explain if you ask :D)

function Start(){

DoorLocked.SetActive(true);
DoorUnlocked.SetActive(false);
ForCorrutine = false;
}

function Update (){
if ( Input.GetMouseButtonDown(0)&& InRange){
GetComponent.<MeshRenderer>().enabled = false;
DoorLocked.SetActive(false);
DoorUnlocked.SetActive(true);
GetComponent.<AudioSource>().Play();
ForCorrutine = true;
}
}

function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == “Player”) {
InRange = true;
if(ForCorrutine == true){                                                                                                 yield WaitForSeconds(1);
Destroy(gameObject);
}
}
}
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == “Player”) {
InRange = false;
}
}

Hoped this helped for any one that was struggling with Key Pick-up and unlock Door Code! 😀