Search Unity

Question Why is the button blinking in white

Discussion in 'Scripting' started by pKallv, Aug 5, 2020.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    I am trying to get a button to blink in red and green (background color) but it blinks in red and white? The button image color is set to white in editor. I have changed colors in the editor but I do not get the blinking in the colors I need.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class BlinkLock : MonoBehaviour
    7. {
    8.     private Button btn_Lock;
    9.     private Color green = new Color(3, 65, 2, 255);
    10.  
    11.     void Start()
    12.     {
    13.         btn_Lock = gameObject.GetComponentInChildren<Button>();
    14.  
    15.         StartCoroutine(BlinkButton());
    16.     }
    17.  
    18.     public IEnumerator BlinkButton()
    19.     {
    20.         while (true)
    21.         {
    22.             btn_Lock.image.color = Color.red;
    23.             yield return new WaitForSeconds(.5f);
    24.  
    25.             btn_Lock.image.color = green;
    26.             yield return new WaitForSeconds(.5f);
    27.         }
    28.     }
    29. }
    30.  
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Color uses RGBA values in the range of 0 - 1, so
    new Color(3, 65, 2, 255);
    is being clamped to (1,1,1,1) which is white.
     
  3. johnc81

    johnc81

    Joined:
    Apr 24, 2019
    Posts:
    142
    Hi,

    Change it to:

    Code (CSharp):
    1. private Color green = new Color(0.3f, 0.65f, 0.2f, 1);
    J
     
    pKallv likes this.
  4. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    ahhh off course I knew that, I am an idiot, tx
     
  5. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    348
    If you want to use your byte values instead of floats, using Color32 is an option.

    Code (CSharp):
    1. private Color green = new Color32(3, 65, 2, 255);