- Home /
How to make a Keypad for a locked door
So i have this project where i have to create a code locked door, and then have the player approach it and put in a password. If the password is correct then the door opens, and if not then the password field just resets.
using UnityEngine;
using System.Collections;
public class KeyPad: MonoBehaviour {
public Texture2D icon;
void OnGUI () {
GUI.Box (new Rect (10,10,300,300), new GUIContent(icon));
}
}
so far this is the only code i have finished and working, and even then i need to find a way to keep this hidden until the player passes through a trigger near the door. I have no coding skills and honestly dont understand coding much but this is something i have to do for a class. i would appreciate any and all help that could be offered.
As your next step, play with GUI.Button() as one way to display the buttons for your keypad.
i am using a photoshop image as a keypad as per my instructions in the assignment. what i have to do now it put a password field into this so that the player can input a 5 digit password on the keyboard and then press enter. after that the password will be compared to a pre written one. if they match an animated door is activated and the key pad gui is deactivated, if they dont match then the password field just resets so they can try again. but without seeing the code to do these things specifically i cant really work with just the walkthrough stuff on the site. its just not how my brain works. please if anyone can provide me with examples of code that i could use. they dont even have to do specifically what i need just something i can build off of, like how to make a password field accept keyboard input, and or how to set its password and such. im a total novice and my $$anonymous$$cher is of little help.
You should add this text as a comment to my answer. (Click more -> convert to comment.) As for your problem, it's not just solved in a few lines of code and we have nothing to start out with. Just try yourself, and if you get stuck come back. I don't think someone will completely write this for you.
As @Unitraxx mentions, new information should not be entered as a comment to your original question or as an edit to your original question. Answers are for attempts to answer your question. Now your question is marked as having two answers and will therefore have far less traffic. You can convert this question to an answer using the "more" dropdown.
Given your new description, you need to look at either GUI.TextField() or GUI.PasswordField() depending on the specification in your assignment. After you have the field up and running, Google something "Unity3d GUI.Textfield Enter" to figure out how to capture the enter key. $$anonymous$$ake an attempt to code it yourself. If you get stuck, come back with your attempt.
alright after looking at the pages you guys recommended i have a decent update for my code. the password field appears where it needs to and receives input. There are only 2 things i have left to get working for this to be ready. i need to make it so that the gui only appears while a player is inside a certain trigger, and while i have code for capturing one of the return keys for some reason it doesnt seem to be working.
using UnityEngine;
using System.Collections;
public class $$anonymous$$eyPad: $$anonymous$$onoBehaviour {
public Texture2D icon;
public string SetPassword = "12345";
public string PlayerPassword = "";
void OnGUI () {
GUI.Box (new Rect (10,10,300,300), new GUIContent(icon));
PlayerPassword = GUI.PasswordField (new Rect(60, 60, 200, 35), PlayerPassword, "*"[0],35);
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$eypadEnter))
{
if(PlayerPassword == SetPassword)
{
transform.Translate(Vector3.left * 2 * Time.deltaTime);
}
}
}
}
i was thinking of using a GUI.Enable set up but i wouldn't know how to code a trigger for it since all the triggers i know work with game objects not scripts. oh and does anyone know how to change the text size in a password field?
Answer by Unitraxx · Apr 11, 2013 at 02:25 AM
Coding is a fun and useful skill to have, I suggest you just dig into some tutorials :) This GUI scripting guide is pretty useful and should get you started with your specific problem : http://docs.unity3d.com/Documentation/Components/GUIScriptingGuide.html
Basically the callback of a button should append the number that button presents to a list (thus this list contains all buttons that are already pressed). You can then compare this list to your password to see if it's correct. :)
Answer by HelloDerr90 · Apr 12, 2013 at 05:11 AM
To get the GUI to pop up when you are near the door I would add a collider to the door. Script a trigger event that allows the GUI to open when you enter the collider and close the GUI when you exit the collider.
Then in your GUI I would add the GUI buttons and assign each button a number. The player than can input a code and your script can check to see if it is correct and then open the door if it is.
thank you for the advice, my current coding should cover all of that, and as per a previous comment i was told not to use buttons for input but the keyboard.
Your answer
![](https://koobas.hobune.stream/wayback/20220613100101im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help with making a triggered gui message 1 Answer
Distribute terrain in zones 3 Answers
UI button doesn't appear - c# 3 Answers
gui label works in js but not in cs 1 Answer
Multiple Cars not working 1 Answer