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";
}
}
}
}
}