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

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

    public float alpha = 0.5f;

    // Use this for initialization
    void Start () {

    }
   
    // Update is called once per frame
    void Update () {

        SpriteRenderer spRend = GetComponent<SpriteRenderer>();
        Color col = spRend.color;
        spRend.color = col;
        col.a =  alpha;
}

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.

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.

I thought I did that and it still doesnt work

void Update () {
        SpriteRenderer spRend = GetComponent<SpriteRenderer>();
        Color col = spRend.color;
        spRend.color = col;
        col.a =  alpha1;
        if (Input.GetKeyDown ("g")) {

            col.a = alpha2;
            Debug.Log("it is g");
        }

        if (Input.GetKeyDown ("h")) {
            Debug.Log("it is d");
            col.a = alpha3;
        }
        if (Input.GetKeyDown ("j")) {
            Debug.Log("it is j");
           
            col.a = alpha3;
        }

I tried that my good sir but it still didnt work :confused:

    SpriteRenderer spRend = GetComponent<SpriteRenderer>();
        Color col = spRend.color;
        col.a =  alpha1;
        spRend.color = col;

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.

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

    private float  alpha1 = 1.0f;
    private float alpha2 = 0.5f;
    private float  alpha3 = 0.0f;

    // Use this for initialization
    void Start () {

    }
   
    // Update is called once per frame
    void Update () {
        SpriteRenderer spRend = GetComponent<SpriteRenderer>();
        Color col = spRend.color;
        col.a =  alpha2;
        spRend.color = col;

        bool one = Input.GetKeyDown (KeyCode.G);
        bool two = Input.GetKey(KeyCode.H);
        bool three = Input.GetKeyUp(KeyCode.J);

        if (one) {


            col.a = alpha3;
            Debug.Log("Alpha: " + alpha3);
            Debug.Log("it is 3");
        }

        if (two) {
            Debug.Log("Alpha: " + alpha2);
            Debug.Log("it is 2");
            col.a = alpha2;
        }

        if (three) {
            Debug.Log("Alpha: " + alpha1);
            Debug.Log("it is 1");
            col.a = alpha1;
        }

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}

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.