I want to change the transparency or visibility (alpha) with a UI button
Hi, I am trying to enable an UI Button, but I cant....I want to enable my script with the function (On Click) adding the Update function. But it doesn't work.
Basically I want to change the transparency or visibility (alpha) with this button
Please any advice is more than welcome.
private var a : float = 1; // alpha starts at 1
var fadeSpeed : float = 2;
private var Green : b!oolean = true;
function Update(){
if(Green == true){
GetComponent.<UI.Text>().material.color.a = Mathf.Lerp(GetComponent.<UI.Text>().material.color.a, 1, Time.deltaTime * fadeSpeed);
}else{
GetComponent.<UI.Text>().material.color.a = Mathf.Lerp(GetComponent.<UI.Text>().material.color.a, 0, Time.deltaTime * fadeSpeed);
}
}![alt text][1]
[1]: /storage/temp/56618-capture.jpg
Answer by allenallenallen · Oct 21, 2015 at 01:14 PM
OnClick() only works with a function you created yourself.
Make a new function called ChangeAlpha() or soemthing and copy the contents in Update() into it.
function ChangeAlpha(){
// if(Green == true){...... blah blah blah blah
}
Then you'll see the option to call it in the editor menu.
On a side note, you don't need to write "Green == true". You just need to write "Green"
if (Green){
}
else{
}
Hi allenallenallen,
Thanks for your replay.
I did make the changes, but when I hit the button the alpha only change a little, I have to hit the button several times in order to make the text invisible.
Is there other think I can do?
Thanks for your help!
private var a : float = 1; // alpha starts at 1
var fadeSpeed : float = 1;
private var Green : boolean = true;
function ChangeAlpha(){
if(Green){
GetComponent.<UI.Text>().material.color.a = $$anonymous$$athf.Lerp(GetComponent.<UI.Text>().material.color.a, 0, Time.deltaTime * fadeSpeed);
}
else{
GetComponent.<UI.Text>().material.color.a = $$anonymous$$athf.Lerp(GetComponent.<UI.Text>().material.color.a, 1, Time.deltaTime * fadeSpeed);
}
}
Do you want it to happen instantly or fade in/out?
Fade: http://answers.unity3d.com/questions/225438/slowly-fades-from-opaque-to-alpha.html
Use a simple boolean.....
Learn some simple coding even if you're strictly a 3D modeler.
if(Green){
GetComponent.<UI.Text>().material.color.a = 0;
Green = false;
}
else{
GetComponent.<UI.Text>().material.color.a = 1;
Green = true;
}
Basically I want to hit one time the button and make invisible the text and hit again the button and make the text visible again.