- Home /
Door key code
Hey people I'm new to java script so kind of stuck on this one. I have a door that will have a keycode panel on it and when the player gets to the door he will enter the 4 number code then the door will animate open. so far i have the GUI code panel on screen but when the code is entered nothing happens. I want the codedooranimation clip to play.
var secretCode = new Array(1,3,3,7);
var playerEntry = new Array();
function OnGUI () {
GUI.Box (Rect (10,10,300,110), "Enter the security code");
if(GUI.Button(Rect (110,30,25,20), "1")){ playerEntry.Push(1); }
else
if(GUI.Button(Rect (135,30,25,20), "2")){ playerEntry.Push(2); }
else
if(GUI.Button(Rect (160,30,25,20), "3")){ playerEntry.Push(3); }
else
if(GUI.Button(Rect (110,50,25,20), "4")){ playerEntry.Push(4); }
else
if(GUI.Button(Rect (135,50,25,20), "5")){ playerEntry.Push(5); }
else
if(GUI.Button(Rect (160,50,25,20), "6")){ playerEntry.Push(6); }
else
if(GUI.Button(Rect (110,70,25,20), "7")){ playerEntry.Push(7); }
else
if(GUI.Button(Rect (135,70,25,20), "8")){ playerEntry.Push(8); }
else
if(GUI.Button(Rect (160,70,25,20), "9")){ playerEntry.Push(9); }
else
if(GUI.Button(Rect (135,90,25,20), "0")){ playerEntry.Push(0); }
if(playerEntry==secretCode){ print("Entered the correct code!");
animation.Play ("codedooropen");
}
else{ playerEntry = new Array(); print("Entered the wrong code!"); }
}
Answer by mpintar · Aug 29, 2011 at 02:14 PM
From what I can tell your problem is
if(playerEntry == secretCode)
arrays are reference types so when trying to compare them with the equality operator it won't work.
try looping through the array and comparing each individual value in both arrays
Edited to show example loop through the array:
for(int i = 0; i < playerEntry.length; i++) {
if(playerEntry[i] != secretCode[i]){
isCorrectCodeEntered = false;
}
}
can you show me and example please? im brand new to java so still trying to get my head around it all. when i read a script i can kind of understand what its doing but writing code is very difficult for me still. I suppose the more i learn the easier it will become. thanks
Have a look at this Stack overflow discussion on comparing arrays in c#
http://stackoverflow.com/questions/713341/comparing-arrays-in-c
If the code discussed in the link is a little complicated basically what you have to do is loop through on of your arrays and then compare the value at each index, if any of the values don't match then they are not equal and therefore the code entered is incorrect.
is there a simple way of having a locked door that can only be accessed with a key of some kind. maybe the key code option is a bit advanced for a beginner. is it possible to maybe push a button in one location that opens a door in another location?
Well, having a button in one location that opens a door somewhere else is exactly as hard as opening a door right next to it- it's simply a matter of moving the button!
is that reply supposed to be as cheeky as it sounds? i didnt know how to open a door with a button no matter location of either :p i do now tho and got the code lock to work anyway.
Answer by gordon230883 · Sep 21, 2011 at 02:46 PM
Ok i finally got a code door to work with this
var doorCode : String = "1234";
var stringToEdit : String; var buttonMessage : String = "Access Denied"; var showMessage : boolean;
function OnTriggerEnter(collision: Collider){ showMessage = true;
}
function OnTriggerExit(collision: Collider){ showMessage = false;
}
function OnGUI(){
if(showMessage){
stringToEdit = GUI.TextField (Rect (20, 100, 75, 25), stringToEdit, 4); if(stringToEdit == doorCode){ buttonMessage = "OpenDoor"; }else{ buttonMessage = "Enter code"; } if(GUI.Button(Rect(95, 100, 95, 35), buttonMessage) && stringToEdit == doorCode){ animation.Play("doorwithcodeanimation"); }
} }
Your answer
Follow this Question
Related Questions
key to open gate 1 Answer
How to code a one way door 1 Answer
Help scripting Combination lock/ pressure plates 1 Answer
secret code to open door with sound emission 0 Answers
I am trying to make a door, when using this script nothing happens. 0 Answers