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 Superdsgross · May 12, 2015 at 01:31 AM · c#buttonclick

Click a Button through a script

I've tried a bunch of different methods I've found on here to get this to work but none of them are. I've tried

Button.SendMessage("OnClick"); Button.SendMessage("OnClick", SendMessageOptions.RequireReceiver); Button.GetComponent().ClickMe;

I don't really understand how the listener aspect is supposed to work either (do I need to but the listener on a script attached to the button?). If anybody can help me understand how to use this I'd really appreciate it. Thanks!

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 Umresh · May 12, 2015 at 05:15 AM 1
Share

Which button are you talking about? if its unity UI button then see this

avatar image Superdsgross · May 12, 2015 at 11:37 AM 0
Share

Yeah sorry that I didn't make it clear. Its a GUI button

1 Reply

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

Answer by UltimateTactix · May 12, 2015 at 10:03 AM

I myself, whenever not using a Unity GUI button but rather created a button from scratch, add a plane to the scene and give it the text of the button and write a script for it.

The script should contain OnMousehover() as a function if you want to add something along the lines of a sound when you go over it and OnMouseDown() should contain your logic for what the button should do.

I hope I explained well enough, if not let me know and I'll provide an example script.

Tip: You can use booleans to keep track of what buttons are pressed and so on to prevent bugs.

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 Superdsgross · May 12, 2015 at 11:40 AM 0
Share

I've also tried to execute the button's effect manually but then I run into the issue of making it non-interactable after its first use. Button.GetComponent(UnityEngine.UI.Button).interactable(false); -this doesn't work

avatar image UltimateTactix · May 12, 2015 at 11:42 AM 0
Share

That's easy, just add a bool to your script that's set to false by default and when you execute the script by triggering the button set it to true. Then add an if statement that no longer allows the button to function unless the bool is reset to false.

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

Multiple Cars not working 1 Answer

How to make unity expect a click 0 Answers

Distribute terrain in zones 3 Answers

Trigger a Button Click 2 Answers

How to implement Clicking in Unity-GearVR by pointing Light??? 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