Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

how do I change the opacity(alpha value) of a 2d sprite ui

Discussion in 'Scripting' started by xxfmxx, Apr 1, 2015.

  1. xxfmxx

    xxfmxx

    Joined:
    May 28, 2014
    Posts:
    32
    so basically I am trying to make my sprite see through via code but for some reason its not working
    I essentially have the alpha channel of the sprite accessible through a public variable to see if anything was happening but it wasnt ....help please as I need this for a project this week

    I found this website and everything the person is saying makes sense to me ..but it just dont want to work

    http://unity.grogansoft.com/change-a-sprites-transparency/

    here is a snippet of my code its the same as the website only I made the alpha value a public variable
    or perhaps my brain is going about this all wrong ...any helps would be greatly appreciated something this simple shouldnt be causing me this much trouble :.../ o and I also get no errors
    Code (CSharp):
    1.     public float alpha = 0.5f;
    2.  
    3.     // Use this for initialization
    4.     void Start () {
    5.  
    6.     }
    7.    
    8.     // Update is called once per frame
    9.     void Update () {
    10.  
    11.         SpriteRenderer spRend = GetComponent<SpriteRenderer>();
    12.         Color col = spRend.color;
    13.         spRend.color = col;
    14.         col.a =  alpha;
    15. }
     
  2. Mr-Mud

    Mr-Mud

    Joined:
    Mar 8, 2015
    Posts:
    37
    My assumption is that the order of your code is wrong. First you get a copy of the color, then you assign a copy of the copy, lastly you change a value on the original copy. As a result, the changes won't have an effect.

    Switch line 13 and 14 (of your snippit). That should do the trick.
     
  3. Notter

    Notter

    Joined:
    Mar 8, 2015
    Posts:
    65
    You've created a variable of type "Color", then toy proceed to change it's alpha.
    and that's it.

    like Mr. Mud said, once you've set your new color, only then you assign it to your object.
     
  4. xxfmxx

    xxfmxx

    Joined:
    May 28, 2014
    Posts:
    32
    I thought I did that and it still doesnt work


    Code (CSharp):
    1. void Update () {
    2.         SpriteRenderer spRend = GetComponent<SpriteRenderer>();
    3.         Color col = spRend.color;
    4.         spRend.color = col;
    5.         col.a =  alpha1;
    6.         if (Input.GetKeyDown ("g")) {
    7.  
    8.             col.a = alpha2;
    9.             Debug.Log("it is g");
    10.         }
    11.  
    12.         if (Input.GetKeyDown ("h")) {
    13.             Debug.Log("it is d");
    14.             col.a = alpha3;
    15.         }
    16.         if (Input.GetKeyDown ("j")) {
    17.             Debug.Log("it is j");
    18.            
    19.             col.a = alpha3;
    20.         }
    21.  
     
  5. xxfmxx

    xxfmxx

    Joined:
    May 28, 2014
    Posts:
    32
    I tried that my good sir but it still didnt work :/

    Code (CSharp):
    1.     SpriteRenderer spRend = GetComponent<SpriteRenderer>();
    2.         Color col = spRend.color;
    3.         col.a =  alpha1;
    4.         spRend.color = col;
    5.  
     
  6. Mr-Mud

    Mr-Mud

    Joined:
    Mar 8, 2015
    Posts:
    37
    In your initial post, you did not specify alpha1, therefore I don't know its value. Are you sure it is somewhere between 0 and 1? If not, you could check its value using something like Debug.Log("Alpha: " + alpha1). Depending on the result when you run your code, you could figure the following:
    • 1 and up: No wonder you don't see anything change; it becomes completely opaque.
    • 0 or below: You did not assign a different value, but the image should disappear completely.
    • Between 0 and 1: ... I don't know what would be causing this, maybe you assigned a material that does not support alpha; but that is just guessing.
    • No output: Either you did not attach the component to the object, or it is disabled.
     
  7. xxfmxx

    xxfmxx

    Joined:
    May 28, 2014
    Posts:
    32
    the material is the sprite-default material
    and the alpha 1 2 3 are basically 1.0f 0.5f and 0.0f in that order
    soo I do see a change when I set the col.a to alpha 2 or 3 i see a change
    but when I try make the alpha change when I press a button the alpha value doesnt change
    but in the debug log the buttons appear to be working but the alpha level isnt changing

    here is a pic of my debug log
    http://postimg.org/image/cow3vwdvn/

    this is what i have now
    Code (CSharp):
    1.     private float  alpha1 = 1.0f;
    2.     private float alpha2 = 0.5f;
    3.     private float  alpha3 = 0.0f;
    4.  
    5.     // Use this for initialization
    6.     void Start () {
    7.  
    8.     }
    9.    
    10.     // Update is called once per frame
    11.     void Update () {
    12.         SpriteRenderer spRend = GetComponent<SpriteRenderer>();
    13.         Color col = spRend.color;
    14.         col.a =  alpha2;
    15.         spRend.color = col;
    16.  
    17.         bool one = Input.GetKeyDown (KeyCode.G);
    18.         bool two = Input.GetKey(KeyCode.H);
    19.         bool three = Input.GetKeyUp(KeyCode.J);
    20.  
    21.         if (one) {
    22.  
    23.  
    24.             col.a = alpha3;
    25.             Debug.Log("Alpha: " + alpha3);
    26.             Debug.Log("it is 3");
    27.         }
    28.  
    29.         if (two) {
    30.             Debug.Log("Alpha: " + alpha2);
    31.             Debug.Log("it is 2");
    32.             col.a = alpha2;
    33.         }
    34.  
    35.         if (three) {
    36.             Debug.Log("Alpha: " + alpha1);
    37.             Debug.Log("it is 1");
    38.             col.a = alpha1;
    39.         }
     
  8. Brominion

    Brominion

    Joined:
    Sep 30, 2012
    Posts:
    48
    line 15 does not create a magic link between col and spRend.color, it is a one time assignment. you will have to make that assignment each time you change col. ( or move it to after all three if statements)

    secondly you will want to set your bools (called one, twoo , three) to false after changing the alpha or you will execute several of the if statements. {edit, by the nature of your input handling, perhaps it is intentional that you can press several key at the same time? if so ignore this part}
     
  9. xxfmxx

    xxfmxx

    Joined:
    May 28, 2014
    Posts:
    32
    my god that make so much sense ..I am such a newb ..it works now .. i can figure out the rest probably but spanks you so much ...my brain is working again.