Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Oct 30, 2013 at 08:50 PM by WhipJr for the following reason:

commands.close.hint.6

avatar image
0
Question by WhipJr · Aug 11, 2011 at 10:56 PM · c#inputbutton

can i "click" a button by pressing a key or gamepad button?

im making a pause menu and i want to have each option labeled with the designated button next to it, but im not sure the script i could use to "click" an on screen button.

i've already tried just labeling the buttons and having the corresponding button do what i want it to but that just makes my pause menu close immediately.

~C#~

     void Update() {
     
     
     if (Input.GetButtonUp("A"))
         
         Time.timeScale = 1.0f;
         ((Behaviour)player.GetComponent("MouseLook")).enabled = true;
         ((Behaviour)cam.GetComponent("MouseLook")).enabled = true;
         ((Behaviour)cam.GetComponent("ShowSkin")).enabled = false;
      
     
 }

ShowSkin is the name of my script with the GUI... but if i add this, as soon as i open my pause menu it closes instantly.

2 ways to answer this question.

  1. tell a way to "click" a button with script

  2. help me fix my script here so it doesn't close immediately.

please help

Comment
Add comment · Show 2
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 Waz · Aug 11, 2011 at 11:07 PM 0
Share

Unrelated tip: use the generic GetComponent:

 // C#
 player.GetComponent<$$anonymous$$ouseLook>().enabled = true;
 // JS
 player.GetComponent.<$$anonymous$$ouseLook>().enabled = true;

It's more efficient and more readable since no need to cast.

avatar image Waz · Aug 11, 2011 at 11:12 PM 0
Share

Please go back and show us when you tried to do it in your OnGUI, because that was the correct approach, whereas what you have now looks like you are heading off in the wrong direction. Having a button shortcut a GUI button is pretty straightforward, so you must have made a small mistake back then (eg. put the GUI.Button after the Input.GetButtonUp in the if predicate).

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Molix · Aug 11, 2011 at 11:05 PM

You are missing braces around the body of your if statement, so only the first statement is conditional on pressing 'A'. i.e. you need:

 void Update() 
 {
     if (Input.GetButtonUp("A"))
     {
         Time.timeScale = 1.0f;
         ((Behaviour)player.GetComponent("MouseLook")).enabled = true;
         ((Behaviour)cam.GetComponent("MouseLook")).enabled = true;
         ((Behaviour)cam.GetComponent("ShowSkin")).enabled = 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 WhipJr · Aug 11, 2011 at 11:07 PM 0
Share

haha thanks... i need to take a break lol... now im having another problem, when i press A to resume i also Jump, is there any way to fix that? besides change the button?

avatar image Waz · Aug 11, 2011 at 11:17 PM 0
Share

You need to not Jump if the GUI is open.

avatar image WhipJr · Aug 11, 2011 at 11:22 PM 0
Share

the button to resume on the GUI is the button to Jump in the game .....

avatar image Molix · Aug 11, 2011 at 11:24 PM 0
Share

You could have your jump check to see if the timeScale is < 1 (in addition to checking the button).

avatar image DoubleDouble · Aug 11, 2011 at 11:25 PM 0
Share

Change either the jump or unpause button, or you'll need something to check the state.

Ex) bool paused = true; if (!paused) Jump()

else Unpause()

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Input - Checking if Input Key/Button Exists 1 Answer

Distribute terrain in zones 3 Answers

Getting Buttons to change stage c# Add Listner 1 Answer

Differentiate between button click and screen click - Pause button not working 3 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