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 /
avatar image
4
Question by PersianKiller · Jan 01, 2018 at 02:12 PM · buttonbeginners

change onclick function via script

First of all I want to tell that I saw lots of related posts and I didn't get it at all !!!! so do not delete this post and don't post links for me :).

so I have a button with no OnClick function. how can I place this script in that button via script? can anyone tell me?for example if player Collides with an object .then this code places in an OnClick function of a button.

 public void Test(){

 Debug.Log ("testttt");
 }

watch this ,it's a short video about what I want. https://streamable.com/a0tq3

any tutotrial or whatever .....

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

Answer by pako · Jan 01, 2018 at 03:53 PM

The UI.Button has an onClick event that gets triggered whenever the button is clicked.

Normally, you'd just add a listener (point to a method) to run when the button gets clicked, in Awake() or Start(), like this:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class ClickExample : MonoBehaviour
 {
     public Button CustomButton; //drag-n-drop the button in the CustomButton field
 
     void Awake()
     {
         CustomButton.onClick.AddListener(CustomButton_onClick); //subscribe to the onClick event
     }
 
     //Handle the onClick event
     void CustomButton_onClick()
     {
         Debug.Log("testtttt");
     }
 }


However, in your case, you want to handle the event only under certain conditions, i.e. if the player collides with an object. To do this, you'll still use most of the above code, but also make sure that the event is not handled if a collision doesn't take place.

So, you could add a conditional statement and use a Boolean.

         //Handle the onClick event Conditionally
         void CustomButton_onClick()
         {
             if(hasCollided) //set hasCollided = true when the player collides with object, and reset to false at an appropriate place.
          {
             Debug.Log("testtttt");
           }
         }



Alternatively, subscribe/unsubscibe the event handler to the event in suitable places, instead of Awake() or Start(). For example:

     private void OnCollisionEnter(Collision collision)
     {
         
         CustomButton.onClick.AddListener(CustomButton_onClick);
 
     }
 
     private void OnCollisionExit(Collision collision)
     {
 
         CustomButton.onClick.RemoveListener(CustomButton_onClick);
 
     }


Let me know if you need any clarifications.

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 deram_scholzara · May 12, 2020 at 04:29 AM 1
Share

Very helpful examples!

Just to clarify for future viewers, the "GetComponent()" part is unnecessary, as CustomButton is already a reference to the Button component.

avatar image pako deram_scholzara · May 12, 2020 at 08:03 AM 0
Share

hehe, I really don't know what I was thinking when I did this, but it's funny you're the only one within 2.5 years to notice and comment about it! :-) In fact, the local btn variable is redundant too. I edited my response to make the usage clearer. Thanks!

avatar image Hefaz · Jan 28, 2021 at 12:08 PM 0
Share

This helped me too much. thanks

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

90 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 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

UI bugging after build? 0 Answers

Enemy Select 1 Answer

How to constantly add force to my player when a button is hold? 2 Answers

Moving camera around with GUI buttons 4 Answers

Toggle between animation states with UI button 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