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

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 _!= secretCode*){*_

isCorrectCodeEntered = false;
}
}

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”);
}

}
}