Search Unity

How to make a button return to Normal Color Tint?

Discussion in 'Scripting' started by FBones, Sep 4, 2018.

  1. FBones

    FBones

    Joined:
    Aug 28, 2016
    Posts:
    73
    If I set interactable to false, the "Disabled" color tint on my button is activated.

    For example:

    Code (CSharp):
    1.     public void ShowAsLegal()
    2.     {
    3.         background.color = new Color(0f, 0.3f, 0f);
    4.         myButton.interactable = false;
    5.     }
    Will disable the button and turn it green (assuming that the Disabled color tint is pure White).

    But when I try to undo this to get the button to go back to "Normal" tint, I get unexpected behavior:

    Code (CSharp):
    1.     public void ResetButton()
    2.     {
    3.         myButton.interactable = true;
    4.         background.color = baseColor;
    5.     }
    After "ResetButton()" is called, the button changes to a gray color. The strange thing is that this does not correspond to any color tint. In the editor the following settings show up:

    Image color: Pure White
    Normal color: Pure Black
    Highlighted color: Blue
    Selected color: Red
    Disabled color: Pure White

    So how can it show up as gray?

    Attached is screenshot showing the affected buttons (gray) and the unaffected buttons(black). The rectangular tickmarks above the numbers are the buttons. Their color characteristics are identical.



    The "Color" and "Disabled Color" are both pure White. And the "Normal Color" is pure black. Hard to understand why they are showing up as grey after having their "interactive" property toggled.
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
  3. FBones

    FBones

    Joined:
    Aug 28, 2016
    Posts:
    73
    Thanks, but that script shows how to change the ColorBlock, not how to get the button to go back to its "Normal" state. The inspector is showing the intended ColorBlock, but the behavior does not match.

    I think I'm going to have to choose another method to do what I'm trying to do.
     
  4. LurchUSA

    LurchUSA

    Joined:
    Sep 20, 2017
    Posts:
    21
    After many, many, MANY hours of false leads on various forums, vague Unity documentation and just flat out experimenting I've found a way, albeit a bit retarded, to do this.

    I found a fragment of what I needed to do here: https://answers.unity.com/questions/1365672/buttons-will-not-change-color.html

    In that all you need to do is:

    Code (CSharp):
    1.  exampleButton.enabled = false;
    2. exampleButton.enabled = true;
    Yep, that's right. You toggle the enabled state of the button false, then back to true to "reset" the button colour tint state to normal.

    Retarded like I said and honestly I don't know WHY Unity does not have an API hook to script the "state" of the button, but whatever... this works.

    If the Unity devs get a frick'in clue, they might not have to force us to use stupid workarounds like this.
     
    lmahieu, DDmeow and lslu like this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    I don't think you need to toggle enabled, but if that works for you go ahead.

    I think the reference way is this:

    Code (csharp):
    1. EventSystem.current.SetSelectedGameObject(null);
    I think that underlying problem is that you are changing the state of the button while your finger is still on it, preventing it from seeing your finger leaving. Therefore it still thinks it's selected.
     
    sachitnjk, CarlGreen and MidniteOil like this.