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
1
Question by CRISTALSONIC · May 14, 2017 at 03:11 AM · buttonscript.

how to change a buttons function with a script?

so i got this working already with this line of code

      x.GetComponentInChildren<Button>().onClick.AddListener(delegate{ use_item(name); }) ;

my problem is when i click the button in question it will keep calling the function as long has its clicked. basically after holding the mouse down for 3 seconds it will call the function 107ish times (from my debug log).

how do i stop that? how do i set my button to call a function only once?

on a side note I found "delegate" while looking up syntax but I can not find any thing explaining what it dose. if any one can tell me what it does that would be appreciated.

i forgot to mention that I am working with a list of buttons and that line of code would update the name (string variable) in each one.

so about 9 buttons in all each call the same function but send its own string and i have the ability to change that string in game. that is what i'm trying for

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

Answer by UnityCoach · May 14, 2017 at 08:38 AM

A delegate is a method you subscribe to an event. When you work with C# events, you declare a delegate that represents the signature of the method (the parameters it must be passed), then you subscribe methods that match that signature/delegate. If you don't already implement such method, you can build a delegate and pass it. This is what you do here :

 x.GetComponentInChildren<Button>().onClick.AddListener(delegate{ use_item(name); }) ;

delegate {} is a method with no name that you pass. The only problem with this, is that you have no reference to it to later unsubscribe it with UnityEvent.RemoveListener. Your only option then is to use UnityEventBase.RemoveAllListeners to remove all delegates.

Consider doing this :

 x.GetComponentInChildren<Button>().onClick.AddListener (OnUseItem);
 public void OnUseItem ()
 {
     use_item(name);
 }

Which allows you to later remove it with :

 x.GetComponentInChildren<Button>().onClick.RemoveListener (OnUseItem);

Hope this clarifies things a bit.

Although, I'm pretty sure there's another problem as a button shouldn't be called 100 of times if you simply keep it pressed, as onClick event is called when you release the button.

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 CRISTALSONIC · May 14, 2017 at 03:00 PM 0
Share

yeah its an odd problem thou i forgot to mention that I am working with a list of buttons and that line of code would update the name (string variable) in each one.

so about 9 buttons in all each call the same function but send its own string and i have the ability to change that string in game. that is what i'm trying for

your suggestion looks like it would change the function all to gather but will try to remove the Listener

avatar image CRISTALSONIC CRISTALSONIC · May 20, 2017 at 01:48 AM 0
Share

so i can't store a reference because i would have no way to dynamically change each button on run time aka all 9 buttons would do the same thing even after i changed the task they would still be synchronized but, x.GetComponentInChildren().onClick.RemoveAllListeners(); was able to fix my problem any way evidently i was storing a new Listener with each loop and changing the string of all the Listeners for each button

avatar image U2F · Aug 28, 2019 at 07:44 PM 0
Share

Hey, I tried this way and for some reason it just doesn't work, $$anonymous$$y button onClick event doesn't change, It might be because I am working on android, I am pretty stuck there, Any ideas?

avatar image
0

Answer by CRISTALSONIC · May 14, 2017 at 04:32 PM

ok that dose not let me dynamically change the name variable and its still calling the function to much so I'm back to were i was

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 UnityCoach · May 14, 2017 at 07:58 PM 0
Share

Can you share your loop code?

avatar image CRISTALSONIC UnityCoach · May 14, 2017 at 10:36 PM 0
Share

public void Inventory_update() { int inventory_slot = 0; // ok this loop will check to see if the buttons need to be set with the bool values // ant it will get the extra info with the ref variables

      foreach (GameObject x in Inventory_list)
     {

         int stock = 0;

         Sprite Item_pic=null;

         string name = "";

if (Player_inventory.inventory_set(inventory_slot, ref stock,ref Item_pic,ref name) == true) {

             x.SetActive(true);

             x.GetComponentInParent<Image>().color = player_data.C_color;

             x.GetComponentInChildren<Button>().image.sprite = Item_pic;

             //x.GetComponentInChildren<Button>().onClick.RemoveListener(delegate { use_item(name); });
             x.GetComponentInChildren<Button>().onClick.AddListener(delegate{ use_item(name);}) ;
             x.GetComponentInChildren<Text>().text = stock.ToString();
         }

         else { x.SetActive(false); }
         inventory_slot++;
     }
 }
avatar image CRISTALSONIC CRISTALSONIC · May 14, 2017 at 10:39 PM 0
Share

i don't get this sites formating

Show more comments

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

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

How create detonation system 0 Answers

Change UI button state to "pressed" via script 2 Answers

Referencing a button in script for different game modes in the Space Shooter Tutorial 0 Answers

New to the new UI (buttons) 2 Answers

Basic scripting 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