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
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.
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
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}