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 madmart · May 30, 2014 at 06:51 AM · getbutton

Getting repeated inputs when I want one at a time.

I want to press "e" to select the attack menu, then press "e" again to use an attack. But, if I press "e" both the menu changes AND the attack is confirmed.

 if (currentMenu == "combatMenu"){
     if (Input.GetButtonDown("e")){
         currentMenu = "attackMenu";
     }
 }
 if (currentMenu == "attackMenu"){
     if (Input.GetButtonDown("e")){
         print "Attack Confirmed";
     }
 }

How do I fix this?

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 fafase · May 30, 2014 at 06:55 AM

You need to add a condition to the second.

if the first one is pressed you put the condition to true and then the second needs that condition to run.

Then you could also set the condition back to false after a certain time.

But this is what you are doing but you are doing it the wrong way:

 if (currentMenu == "combatMenu"){
     if (Input.GetButtonDown("e")){
         currentMenu = "attackMenu";
     }
 }
 else if (currentMenu == "attackMenu"){
     if (Input.GetButtonDown("e")){
         print "Attack Confirmed";
     }
 }

The else if makes the second part being ignored if the first one is run. So on the first round, it will go inside the first part and turning your variable. The second part is skipped from the first round. Second round, the first if is false so it goes to check for the second and it works.

Comment
Add comment · Show 4 · 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 madmart · May 30, 2014 at 07:20 AM 0
Share

Adding the 'else' fixed the problem, but I don't understand why it's needed. Shouldn't the second part just return false the first round and skip the input?

avatar image fafase · May 30, 2014 at 07:41 AM 1
Share

Never underestimate how stupid a computer can be. It does things as he sees them.

 if (current$$anonymous$$enu == "combat$$anonymous$$enu"){ // This is true, I get in
     if (Input.GetButtonDown("e")){ // This is true, I get in
         current$$anonymous$$enu = "attack$$anonymous$$enu"; // I change this
     }
 }// I am out of the statement and I continue
 if (current$$anonymous$$enu == "attack$$anonymous$$enu"){ // This is true, since I just change it two lines ago, I get in
     if (Input.GetButtonDown("e")){ // This is also true since it is true for the whole frame
         print "Attack Confirmed"; // I do that even though you don't want it now
     }
 }
avatar image fafase · May 30, 2014 at 07:47 AM 1
Share

Extra advice for free, avoid using string for your state, use an enum. They are faster and less prone to error since you cannot pass whatever value but the one in the enum.

avatar image madmart · May 30, 2014 at 07:57 AM 0
Share

Thank you for the advice! I didn't realize the variable would be changed within the same frame, and that the input condition would remain true for the rest of the frame as well. This clears up a lot.

As for the enums I haven't learned about those yet. This is all experimental code while I $$anonymous$$ch myself scripting. Next thing I do will be to learn all about enums.

avatar image
0

Answer by Andres-Fernandez · May 30, 2014 at 07:03 AM

It's a problem with your logic. Just debug the code with monodevelop, you'll see that in the same frame your currentMenu changes from "combatMenu" to AttackMenu", and as Input.GetButtonDown is true both times you call it (actually it will be true all the times you call it in the same frame) both if statements will be true. Try it this way:

 if (Input.GetButtonDown("e")) {
    if (currentMenu == "combatMenu") {
       currentMenu = "attackMenu";
    } else if (currentMenu == "attackMenu"){
       print "Attack Confirmed";
    }
 }


Comment
Add comment · Show 2 · 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 madmart · May 30, 2014 at 07:27 AM 0
Share

So, is it good practice to always use else statements when I have a list of conditions? For example, here's the same code but in more context:

 if (current$$anonymous$$enu == "combat$$anonymous$$enu"){
     if (e) current$$anonymous$$enu = "attack$$anonymous$$enu"; 
     if (s) current$$anonymous$$enu = "item$$anonymous$$enu";
     if (d) current$$anonymous$$enu = "move$$anonymous$$enu";
     if (f) current$$anonymous$$enu = "magic$$anonymous$$enu";
     if (mouse1) current$$anonymous$$enu = "select$$anonymous$$enu";
 }

Should it ins$$anonymous$$d look like this?

 if (current$$anonymous$$enu == "combat$$anonymous$$enu"){
     if (e) current$$anonymous$$enu = "attack$$anonymous$$enu"; 
     else if (s) current$$anonymous$$enu = "item$$anonymous$$enu";
     else if (d) current$$anonymous$$enu = "move$$anonymous$$enu";
     else if (f) current$$anonymous$$enu = "magic$$anonymous$$enu";
     else if (mouse1) current$$anonymous$$enu = "select$$anonymous$$enu";
 }
avatar image fafase · May 30, 2014 at 08:44 AM 1
Share

Only if you are meant to have one condition at a time.

Actually, in your case I would have used a switch statement which avoid this kind of bug. But you said to be in a learning state so you may not have seen them yet.

So switch and enumeration is what I would go for.

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I designate the string for GetButtonDown? 2 Answers

How can I use multiple GetButtonDown keys simultaneously? 2 Answers

Why is this variable changing for no reason? 1 Answer

Get DPad input value via GetButton instead of GetAxis? 8 Answers

Input.GetMouseButtonDown(0) problem 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