Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by gordon230883 · Aug 29, 2011 at 01:59 PM · doorlock

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!"); } 
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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;
   }
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gordon230883 · Aug 29, 2011 at 02:52 PM 0
Share

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

avatar image mpintar · Aug 29, 2011 at 03:07 PM 0
Share

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.

avatar image gordon230883 · Aug 29, 2011 at 03:24 PM 0
Share

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?

avatar image syclamoth · Sep 21, 2011 at 02:58 PM 0
Share

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!

avatar image gordon230883 · Sep 22, 2011 at 09:24 AM 0
Share

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.

avatar image
0

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

} }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Opening an animated door with a keycard 0 Answers

Making locked gates/doors (C#) 1 Answer

How do you select a root object from a child's script? 1 Answer

Pick up key and unlock a locked door 1 Answer

Door Animation, Script Help Please! 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges