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 /
  • Help Room /
avatar image
0
Question by vittu1994 · Apr 22, 2016 at 07:22 AM · buttonboolonclickif statementcheck

check if-statement/bool when button is pressed?

Hi i have 4 buttons that all use the same function but with different gameobjects:

 public void BuildShip(GameObject ship)
     {
         Instantiate(ship, mousePos, Quaternion.identity);
     }

I know how buttons work and how to call a function with them, however i want to know how to check if that certain button has been pressed. How do i see when OnClick() has been called when clicking the button? How can i use it in a if statement?

What i wanna do is: See if a certain button has been pressed, lets say button2. When button2 has been pressed i wanna for example change a int or call a debug.log etc. Im pretty sure i want to use an if-statement which is called everytime i press the button or i can have a loop to check all the time when a certain button is pressed.

But for now to simply answer this question: I want to know how to see if a button is pressed and how i can check this with the help of an if-statement.

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
1
Best Answer

Answer by Ali-hatem · Apr 22, 2016 at 09:23 AM

with script attached to the Buttons :

 public void Example(){
     switch (gameObject.name) {
     case "Button_1":
         print (gameObject.name);
         break;
     case "Button_2":
         print (gameObject.name);
         break;
     }
 }
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 vittu1994 · Apr 22, 2016 at 11:02 AM 0
Share

Hey thanks! worked like a charm! To make this work i put this script to the buttons then i assign a new OnClick to the buttons with this script and function. It will now know when i click and then check which button it was and print it!

avatar image Ali-hatem vittu1994 · Apr 22, 2016 at 11:56 AM 0
Share

yes now if the button was button1 do button1 stuffs etc.... & you welcome .

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

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

Related Questions

In void bool dont changing 1 Answer

Button object has no attribute OnClick 1 Answer

UI button not working (2D) [Solved] 2 Answers

Is it possible to use a GUI Button (On Click) to force Input keyboard commands like spacebar for Jump? 2 Answers

Button AddListener - pass in a UnityAction 0 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