- Home /
Open door with button and textfield question
Hello,
This is very important for my game. i make a button with the name ' open door ' this is the script that a write
function OnGUI () {
if(GUI.Button(new Rect(95, 100, 95, 35), "open door"))
animation.Play("port test"); }
and a make a textfiel behind the button. the script
var stringToEdit = "123";
function OnGUI () { // Make a text field that modifies stringToEdit. stringToEdit = GUI.TextField (Rect (20, 100, 70, 25), stringToEdit, 4); }
this is a capture of the result
So, i have a door in my game that will be open if the player write the right code for example '' 1235 '' and hit open door button. in other way the open door button become true if the player write the right code but if he write another code the button '' open door '' become false. I think that a should use a static var to call one script from another. id don't know how. How to make this work with a javascript. i'm not good at scripting to a found this hard for me. thanks for any help.
Answer by spinaljack · Aug 25, 2010 at 12:53 PM
To match a string you can use this:
var doorCode : String = "1234"; var stringToEdit : String; var buttonMessage : String = "Access Denied";
function OnGUI(){ stringToEdit = GUI.TextField (Rect (20, 100, 70, 25), stringToEdit, 4); if(stringToEdit == doorCode){ buttonMessage = "OpenDoor"; }else{ buttonMessage = "Access Denied"; } if(GUI.Button(Rect(95, 100, 95, 35), buttonMessage) && stringToEdit == doorCode){ //openDoor } }
Thanks that's work fine. thank you very much. but a have one more problem. i add another line in the end of the script
if(GUI.Button(Rect(95, 100, 95, 35), button$$anonymous$$essage) && stringToEdit == doorCode){ //openDoor animation.Play("port test");
But the door still not open.
Your answer
![](https://koobas.hobune.stream/wayback/20220613072027im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Activate object with key 1 Answer
Help with making his work with a key 1 Answer
Opening, Closing, Keeping door open at proximity 1 Answer
Closing sliding door Raycasting 1 Answer