Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by youngks · May 06, 2013 at 12:09 AM · javascriptguiinputarraygui.button

Need help using GUI.Button/ input storage

It's actually probably more of a javascript question. Basically, I am trying to write a script to apply to a "Safe" object, which the player must open using a number combination.

So I set up a script with a keypad that comes up on the screen when the player hits the designated action button. It's just a bunch of simple GUI.Buttons. I'm trying to figure out how I can store the numbers that the player punches in through the keypad into an array, and check to see if it is the right combination. I've got a basic psudo-code for it, but I'm not very good with javascript, so I was wondering if anyone has experience with this type of script, or if they have any tips?

Here is what I have now (or at least what is referring for to the keypad. it's kind of a mixture between psudo-code and javascript right now... sorry.):

 var intarray = int[4];
 boolean right = false;
 
 function buttonListener(int x)
     {
         if (button1 == pressed)
             intarray[x] = 1;
         else if (button2 == pressed)
             intarray[x] = 2;
         else if (button3 == pressed)
             intarray[x] = 3;
         else if (button4 == pressed)
             intarray[x] = 4;
         else if (button5 == pressed)
             intarray[x] = 5;
         else if (button6 == pressed)
             intarray[x] = 6;
         else if (button7 == pressed)
             intarray[x] = 7;
         else if (button8 == pressed)
             intarray[x] = 8;
         else if (button9 == pressed)
             intarray[x] = 9;
         else if (button0 == pressed)
             /ntarray[x] = 0;
     }
     
     
 
 function OnGUI()
 {
     if (showGUI)
     {
         button1 = GUI.Button(new Rect(50,150,50,50), "1");
         button2 = GUI.Button(new Rect(100, 150, 50, 50), "2");
         button3 = GUI.Button(new Rect(150, 150, 50, 50), "3");
         button4 = GUI.Button(new Rect(50, 200, 50, 50), "4");
         button5 = GUI.Button(new Rect(100, 200, 50, 50), "5");
         button6 = GUI.Button(new Rect(150, 200, 50, 50), "6");
         button7 = GUI.Button(new Rect(50, 250, 50, 50), "7");
         button8 = GUI.Button(new Rect(100, 250, 50, 50), "8");
         button9 = GUI.Button(new Rect(150, 250, 50, 50), "9");
         buttonDash = GUI.Button(new Rect(100, 300, 100, 50), "-"); 
         button0 = GUI.Button(new Rect(50, 300, 50, 50), "0");
         buttonEnter = GUI.Button(new Rect(50, 350, 150, 50), "Enter");
         GUI.Box(new Rect(Screen.width/2-50, 150, 200, 50), "Enter Pin Number");
         
         while(i < 4)
         {
             buttonListener(i)
             i++;
         }
         if intArray== //code
             win
         else
             lose
         for(i=0, i<4, i++)
         {
             if(array[i]==code[i])
                 right = true;
             else
                 break;
         }
     }

All help is welcome :) Thanks

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · May 06, 2013 at 12:20 AM

Rather than use integers, just use a string. No need for your array or listener code. So if you had a input string:

 if (GUI.Button(new Rect(50,150,50,50), "1"))
     inputString += "1";

When they press the enter button:

 if (GUI.Button(new Rect(50, 350, 150, 50), "Enter")) {
     if (inputString == "5426") {
         // Do whatever for a correct combinaton
     }
     else {
        inputString = "";   // Clear for next try
       // Dispaly error message???
     }
 }
Comment
Add comment · Show 3 · 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 youngks · May 06, 2013 at 07:37 PM 0
Share

@robertbu

robertbu, I really like this idea and I am trying to implement it. Here is what I have, and I am just trying to get it to return the result to the console. Any idea why it won't work? I can't really see anything, unless inputString shouldn't be a variable, and is actually something im supposed to import?

 var correct = false;
 var inputString = "";
 
 
 function OnGUI()
 {
     if (showGUI)
     {
         if (GUI.Button(new Rect(50,150,50,50), "1"))
             inputString += "1";
         else if (GUI.Button(new Rect(100,150,50,50), "2"))
             inputString += "2";
         else if (GUI.Button(new Rect(150,150,50,50), "3"))
             inputString += "3";
         else if (GUI.Button(new Rect(50,200,50,50), "4"))
             inputString += "4";
         else if (GUI.Button(new Rect(100,200,50,50), "5"))
             inputString += "5";
         else if (GUI.Button(new Rect(150,200,50,50), "6"))
             inputString += "6";
         else if (GUI.Button(new Rect(50,250,50,50), "7"))
             inputString += "7";
         else if (GUI.Button(new Rect(100,250,50,50), "8"))
             inputString += "8";
         else if (GUI.Button(new Rect(150,250,50,50), "9"))
             inputString += "9";
         else if (GUI.Button(new Rect(50,300,50,50), "0"))
             inputString += "0";
         GUI.Box(new Rect(Screen.width/2-50, 150, 200, 50), "Enter Pin Number");
         
         
         if (GUI.Button(new Rect(50, 350, 150, 50), "Enter")) {
             if (inputString == "5317") {
             // Do whatever for a correct combinaton
                 correct = true;
                 return correct;
                     }
             else {
                inputString = "";   // Clear for next try
               // Dispaly error message???
               GUI.Box(new Rect(Screen.width/2-50, 250, 200, 50), "Try Again");
             }
avatar image robertbu · May 07, 2013 at 02:05 AM 0
Share

Your keypad/combo logic is fine. Your display logic for your error message has issues. With GUI, you have to display the error message every frame. The way it is structured now, it will only display the message for the single frame the enter is pressed. Here are a few quick and dirty changes to your code to display messages:

 #pragma strict
 
 var correct = false;
 var inputString = "";
 var incorrect = false;
  
 function OnGUI()
 {
     if (true)
     {
         if (GUI.Button(new Rect(50,150,50,50), "1"))
            inputString += "1";
         else if (GUI.Button(new Rect(100,150,50,50), "2"))
            inputString += "2";
         else if (GUI.Button(new Rect(150,150,50,50), "3"))
            inputString += "3";
         else if (GUI.Button(new Rect(50,200,50,50), "4"))
            inputString += "4";
         else if (GUI.Button(new Rect(100,200,50,50), "5"))
            inputString += "5";
         else if (GUI.Button(new Rect(150,200,50,50), "6"))
            inputString += "6";
         else if (GUI.Button(new Rect(50,250,50,50), "7"))
            inputString += "7";
         else if (GUI.Button(new Rect(100,250,50,50), "8"))
            inputString += "8";
         else if (GUI.Button(new Rect(150,250,50,50), "9"))
            inputString += "9";
         else if (GUI.Button(new Rect(50,300,50,50), "0"))
            inputString += "0";
         GUI.Box(new Rect(Screen.width/2-50, 150, 200, 50), "Enter Pin Number");
         
         if (correct)
            GUI.Box(new Rect(Screen.width/2-50, 250, 200, 50), "You are correct");
            
         if (incorrect) {
             if (inputString == "")
                GUI.Box(new Rect(Screen.width/2-50, 250, 200, 50), "Try Again");
             else
                incorrect = false;
         }
  
  
         if (GUI.Button(new Rect(50, 350, 150, 50), "Enter")) {
            if (inputString == "5317") {
             // Do whatever for a correct combinaton
                correct = true;
                //return correct;
                
            }
          else {
             inputString = "";   // Clear for next try
             incorrect = true;
          
            }
        }
     }
 }    
avatar image youngks · May 10, 2013 at 01:57 AM 0
Share

This worked! Thank you so much for the help!

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Text.Field erratically updating 0 Answers

Open gui if player clicks on button 2 Answers

drawing GUI or Instantiate an object to highlight multiple object 2 Answers

What type of Array should I use? 1 Answer

Arrange ints from biggest to smallest and display them in a table 1 Answer


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