Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Change The Alpha Of A Button In C# Script?

Discussion in 'Unity UI & TextMesh Pro' started by JeZxLee, Aug 12, 2018.

  1. JeZxLee

    JeZxLee

    Joined:
    Jul 24, 2016
    Posts:
    139
    Hi,

    I am trying to change the alpha of a button in C# script.
    Is the above possible, if yes then please provide an example...
    Thanks!

    Jesse
     
  2. JeZxLee

    JeZxLee

    Joined:
    Jul 24, 2016
    Posts:
    139
  3. JeZxLee

    JeZxLee

    Joined:
    Jul 24, 2016
    Posts:
    139
    Here is the script I have attached to the button(it compiles, but does nothing?):
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class ScreenFadeScript : MonoBehaviour {
    5.    // Use this for initialization
    6.    void Start () {
    7.        Globals.ScreenFadeAlpha = 255;
    8.        Globals.ScreenFadeStatus = Globals.ScreenFadingIn;
    9.    }
    10.    // Update is called once per frame
    11.    void Update () {
    12.        if (Globals.ScreenFadeStatus == Globals.ScreenFadingIn)
    13.        {
    14.            if (Globals.ScreenFadeAlpha > 0)
    15.            {
    16.                Globals.ScreenFadeAlpha-=1;
    17.                Color tmp = GetComponent<UnityEngine.UI.Image>().color;
    18.                tmp.a = Globals.ScreenFadeAlpha;
    19.                GetComponent<SpriteRenderer>().color = tmp;
    20.            }
    21.        }
    22.    }
    23. }
    24.  
     
  4. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    396
    hi,

    please not that the Color struct is using float values, not bytes.
    For Alpha it means:
    - a value of 0 is invisible
    - a value of 1 is opaque
    - everything greater than 1 is clamped to one.