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
0
Question by TheDogePwner · Aug 18, 2016 at 04:49 PM · c#uibutton

Cannot change Button interactable in a UnityAction

Hello,

I have a UnityEngine.UI.Button which, when clicked, increments a value and then makes itself disabled. This is what the code looks like:

 Button button = CreateButton();

 button.onClick.AddListener(() => {
     Debug.Log("Hello World!");
     button.interactable = false;
 });

This prints "Hello World!" to the console when the button is clicked, but it does not disable itself. The following code works and disables the button before it is pressed:

 Button button = CreateButton();
 button.interactable = false;

 button.onClick.AddListener(() => {
     Debug.Log("Hello World!");
 });

But this is not what I want. How can I get this to work?

UPDATE

It's not referencing the expected button. I have 20 separate canvases, each drawing their own set of buttons. Each canvas is on Screen Space - Camera, and they are all tied to their own cameras. I only have 1 camera active, though, but when I press the buttons, it references a button which is not a child of the canvas that I have visible. To explain this better, here is a screenshot of my hierarchy:

alt text

As you can see, I have several players, each with their own canvas and camera. The first player is the only one whose camera is active. But, for some reason, when I click the buttons drawn on the screen, they reference the buttons of other player's buttons. This is very strange... the canvases should only be rendering to their own player's cameras!

The buttons are never given a different value within my C#. The only time they ever are assigned a value is when they are initialized.

hierarchy.png (10.7 kB)
Comment
Add comment · Show 5
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 TheDogePwner · Aug 18, 2016 at 05:06 PM 0
Share

Also, another interesting little point, hovering over the buttons and clicking them does not change their color, but the EventSystem still registers the button being clicked.

avatar image troien · Aug 18, 2016 at 06:57 PM 0
Share

If you change this line

 Debug.Log("Hello World!");

to this line:

 Debug.Log("Hello World!", button);

does the correct/expected button get highlighted in the hierarchy when you click the "Hello World" in the console log?

Because if not, check if you assign button after you add the listener, button is a reference, so if you assign something else to it afterwards you could end up setting interactible of some other button.

avatar image TheDogePwner · Aug 20, 2016 at 01:03 AM 0
Share

That caused some strange results, see the update in the question.

avatar image troien TheDogePwner · Aug 20, 2016 at 08:42 AM 0
Share

Hmmm... Strange. Normally, this kind of error happens in a situation like this

 Button button;
 
 for (int i = 0; i < Length; i++)
 {
     button = CreateButton();
     button.onClick.AddListener(() =>
     {
          button.interactable = false;
          Debug.Log("Hello World!");
     });
 }

Where you constantly overwrite the button reference with a new value resulting in that when the onClick event gets called, button references the last button created in the for loop resulting in that it doesn't work. The fix there would be to place the Button button; line inside the for loop so that you never assign a new value to an existing reference, but ins$$anonymous$$d declare new reference each time. $$anonymous$$ore info about that subject can be found in this great blogpost.

But sinse you claim that you don't assign anything to button I don't really know what the problem is. Unless you didn't understand me and are actuallly still doing something like what I described above.

avatar image TheDogePwner troien · Aug 20, 2016 at 03:05 PM 0
Share

Nope, that's defenitely not the problem. $$anonymous$$y button only has its value set when it's initialized. Thank you for your help, though.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by meat5000 · Aug 18, 2016 at 05:14 PM

Did you add

 using UnityEngine.EventSystems;
Comment
Add comment · Show 1 · 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 TheDogePwner · Aug 18, 2016 at 05:18 PM 0
Share

Just tried that, didn't change anything. Thanks for the answer, though.

avatar image
0

Answer by Skyler_Hawkins · Aug 19, 2016 at 12:09 AM

I hope this can help out, try playing around with this to get it to fit what you need. I'm pretty new with buttons too, but I had this work for me. I'll write it out step-by-step for anyone that needs help understanding. You just have to make sure you add what you want the click to do in the "On Click ()" at the button of the Inspector.

using UnityEngine; using UnityEngine.UI; //have to have this when working with UI using System.Collections;

 public class MenuButton : MonoBehaviour
 {
     public Canvas Menu;
     public Button ButtonText;
     // add more Public Button as you need i.e public Button Button2Text;
 
 
 
     // this sets up your canvas and button(s) 
     void Start()
     {
         Menu = Menu.GetComponent<Canvas>();
         ButtonText = ButtonText.GetComponent<Button>();
         Menu.enabled = true;
         ButtonText.enabled = true;
 
     }
 
     public void ButtonPress()
     {
         Debug.Log("Hello World!");
         ButtonText.enabled = false;
     }
 
     // add more pulbic voids as need for each button that you need i.e public void Button2Press()
 }


 

Add Script to your Canvas

Add your canvas to the Menu area of the Inspector of your Canvas

Add your Button(s) to the Button area of the Inspector of your Canvas alt text

On your Button Inspector

Click the + button on the "On Click ()"

Add your Canvas to the Area that opens up

Click on drop down that says "No Function"

Go to the bottom and Click MenuButton

Find the Function you created in your script that you want (in this case "ButtonPress")

alt text


button-stuff-2.png (7.4 kB)
button-stuff.png (18.3 kB)
Comment
Add comment · Show 1 · 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 TheDogePwner · Aug 20, 2016 at 01:01 AM 0
Share

$$anonymous$$y code should do the exact same thing as what the editor does, this didn't change anything, but thanks for your answer!

avatar image
0

Answer by DougMcFarlane · Aug 19, 2016 at 12:10 AM

Maybe your listener doesn't have a reference to the button?

Does this work?

  Button button = CreateButton();
  button.onClick.AddListener((button) => {
      Debug.Log("Hello World!");
      button.interactable = false;
  });

If not, check that the listener is referencing the expected button, by changing your debug to:

 Debug.Log("Button: " + button.name);

Comment
Add comment · Show 1 · 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 TheDogePwner · Aug 20, 2016 at 12:52 AM 0
Share

This gave some interesting results. I will edit the question to clarify and explain.

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

80 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

Related Questions

Changing anchor positions on UI? 0 Answers

UIButton highlighted color not working 0 Answers

Help making multiple buttons do a specific set of actions in any order pressed 1 Answer

How to keep your button selected after clicking away ??? 1 Answer

UI Button color 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