- Home /
How to Set Max Numbers for a password
So here is my problem. I have a working GUI Keypad script. When I input the password it opens the doors as it is supposed to. However I have a slight snag. If the player inputs the incorrect password or enters more numbers than the password then it just keeps going infinately. So how do I set it up so that they can enter only a maximum of 4 numbers(Which is the password length) and so that if the number doesn't match it resets? I pasted my code below so ya'll can see it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;
public class KeyPad : MonoBehaviour
{
public string curPassword = "1512";
public string input;
public bool onTrigger;
public bool doorOpen;
public bool keypadScreen;
public Transform player;
void OnTriggerEnter(Collider other)
{
onTrigger = true;
}
void OnTriggerExit(Collider other)
{
onTrigger = false;
keypadScreen = false;
input = "";
}
void Update()
{
if (input == curPassword)
{
stonedoor1.close_1 = false;
stonedoor2.close_2 = false;
doorOpen = true;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
player.GetComponent<FirstPersonController>().enabled = true;
}
}
void OnGUI()
{
if (!doorOpen)
{
if (onTrigger)
{
GUI.Box(new Rect(0, 0, 200, 25), "Press 'E' to open keypad");
if (Input.GetKeyDown(KeyCode.E))
{
keypadScreen = true;
onTrigger = false;
}
}
if (keypadScreen)
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
player.GetComponent<FirstPersonController>().enabled = false;
}
if (keypadScreen)
{
GUI.Box(new Rect(0, 0, 320, 455), "");
GUI.Box(new Rect(5, 5, 310, 25), input);
if (GUI.Button(new Rect(5, 35, 100, 100), "1"))
{
input = input + "1";
}
if (GUI.Button(new Rect(110, 35, 100, 100), "2"))
{
input = input + "2";
}
if (GUI.Button(new Rect(215, 35, 100, 100), "3"))
{
input = input + "3";
}
if (GUI.Button(new Rect(5, 140, 100, 100), "4"))
{
input = input + "4";
}
if (GUI.Button(new Rect(110, 140, 100, 100), "5"))
{
input = input + "5";
}
if (GUI.Button(new Rect(215, 140, 100, 100), "6"))
{
input = input + "6";
}
if (GUI.Button(new Rect(5, 245, 100, 100), "7"))
{
input = input + "7";
}
if (GUI.Button(new Rect(110, 245, 100, 100), "8"))
{
input = input + "8";
}
if (GUI.Button(new Rect(215, 245, 100, 100), "9"))
{
input = input + "9";
}
if (GUI.Button(new Rect(110, 350, 100, 100), "0"))
{
input = input + "0";
}
}
}
}
}
Answer by pako · Oct 23, 2018 at 08:08 AM
You'l need to add a counter, which will increment every time a key is pressed. Then check the complete password, when counter == paswordLength. If that evaluates to true, open the door, if to false, reset the counter to 0, and start over.
e.g. (pseudo code)
int passwordLength = 4;
int counter = 0;
bool passwordCorrect;
while(counter <= passwordLength){
if(counter == passwordLength){
passwordCorrect = CheckPassword();
if(passwordCorrect){
OpenDoor();
}
else
{
counter = 0;
}
}
else
{
GetNextKeyInput(int keyNumber) // keyNumber corresponds to the key position, i.e. 1 for first key, 4 for last key
counter++// this can also be placed wherever it makes the most sense, depending on the rest of your code.
}
}
//This may also return an int rather than void. The int returned could, for example, contain the current total input (1 to 4 numbers), so you can process it as you need. Do it as it makes most sense depending on the rest of your code.
void GetNextKeyInput(int keyNumber){
}
Hey @FoodLover195 , the question was:
So how do I set it up so that they can enter only a maximum of 4 numbers(Which is the password length)
I honestly can't see how your code does that... Anyway, I won't enter into an argument with you.
I thought it was pretty straightforward. If the input string isn't correct, and the length of the input was over 3, then you could do whatever needs to be done in the else if statement. For example reset the input, or have a bool checked that doesn't allow the user to press anything else. Anyway I don't want an arguement either, just felt like you over complicated it by a tonn.
Answer by FoodLover195 · Oct 23, 2018 at 08:01 AM
Hi @Adoyser
You should be able to place a if statement in Update to check if the string is greater than 4 characters.
if (input == curPassword) {
// Correct Password has been entered. Proceed
} else if (input.Length >= 3) {
// Incorrect password has been entered & input length is over 3 characters.
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to make a Keypad for a locked door 2 Answers
Distribute terrain in zones 3 Answers
Leaderboard GUISkin? 1 Answer
Displaying a static variable from another script with OnGUI 1 Answer