- Home /
How to make a code lock in a 3d game
I made a script that used ontriggerstay to make a code lock, the script is supposed to wait for the user to enter a number, in this case I made the only number 0 just to test it. Once you input 0 it sets a Boolean for the first number to false and one for another number to true. I do this so I can enter different numbers for a combination. However when I test my code it correctly accepts the first number and sets the Booleans but refuses to accept the other inputs.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CodeLock : MonoBehaviour {
public string[] password;
public bool[] locks;
// Use this for initialization
void Start () {
locks[0] = true;
}
// Update is called once per frame
void Update () {
}
void OnTriggerStay(Collider other)
{
if(other.tag == "Player")
{
if (Input.anyKeyDown || locks[0] == true){
if(Input.inputString == "0" )
{
password[0] = "0";
locks[0] = false;
locks[1] = true;
}
}
else if (Input.anyKeyDown || locks[1] == true)
{
if (Input.inputString == "0")
{
password[1] = "0";
locks[1] = false;
locks[2] = true;
}
}
else if (Input.anyKeyDown || locks[2] == true)
{
if (Input.inputString == "0")
{
password[2] = "0";
locks[2] = false;
locks[3] = true;
}
}
else if (Input.anyKeyDown || locks[3] == true)
{
if (Input.inputString == "0")
{
password[3] = "0";
locks[3] = false;
}
}
}
}
}
So you want Player to set the combination ? interesting...
public string[] Password = new string[4];
public bool[] Locks = new bool[4];
private void Start()
{
Locks[0] = Set$$anonymous$$eOnly();
}
private void OnTriggerStay(Collider other)
{
if (other.CompareTag("Player") && Input.any$$anonymous$$eyDown)
{
if (Locks[0] && Input.inputString == "0")
{
Password[0] = "0";
Locks[1] = Set$$anonymous$$eOnly();
}
else if (Locks[1] && Input.inputString == "0")
{
Password[1] = "0";
Locks[2] = Set$$anonymous$$eOnly();
}
else if (Locks[2] && Input.inputString == "0")
{
Password[2] = "0";
Locks[3] = Set$$anonymous$$eOnly();
}
else if (Locks[3] && Input.inputString == "0")
{
Password[3] = "0";
Locks[3] = false;
}
}
}
private bool Set$$anonymous$$eOnly()
{
for (var i = 0; i < Locks.Length; i++) Locks[i] = false;
return true;
}
Answer by Bunny83 · Oct 01, 2017 at 12:32 AM
You use the "or" operator in this condition:
if (Input.anyKeyDown || locks[0] == true){
That means you enter the body of this if statement whenever one of the two conditions is true. That means you always enter the first if block when "anyKeyDown" is true. Since it's an else-if-chain none of the other blocks will be executed when you press a button.
It's generally a strange way to check a password. Even if you would correctly use && instead of || this way is not secure. As soon as you type in the first key correctly it will stay in that state until you press the second key correctly even when you type in several wrong keys in between. So the user can simply spam random keys and will eventually get to the end of your chain.
It would be easier to actually collect the entered keys in a List or concat them to a string and then check the string against the password once the input string is equal or longer to the password.
Though which approach would be the best depends on your desired behaviour. How does your player move into the trigger? Does he use WASD or other keys? They can interfer with the password detect code. Should the state be reset once the player exits the trigger area? Do you want to detect a single password or maybe multiple different passwords?
It's usually a good idea to provide user feedback so they know they can enter a password now unless it should be some sort of secret
Thanks, I cant believe I made that mistake, I will go bang my head against my keyboard now
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making lists out of other lists 1 Answer
Lock/Unlock system don't work perfectly 0 Answers
levels are locked again after playing a lower level 0 Answers