- Home /
check alpha of gameObject C#
I have a button that fades into screen using leanTween, however I want to be able to only make the button click-able once it has finished fading it. I saw that the delay functions delay the whole scene so it will delay the fading as well.
I was thinking of using the update() function to check the alpha of the button till it is maxed (or == 1f) but cant find out how to do this, any help please?
Thanks in advance.
Answer by eezSZI · Mar 20, 2015 at 01:06 PM
If you are using a new UI button, you could find the targetGraphic and use the built in crossfade. Or do it manually. Based on your comment code it looks like you are use the UI.Button, in which case it usually has a reference to a Graphic.
UnityEngine.UI.Button b = new UnityEngine.UI.Button();
b.targetGraphic.CrossFadeAlpha(0, .6f, true);
// or check/set alpha/color directly
b.targetGraphic.color.a = 0;
this is the best way to do what I wanted so I accepted this as the answer, however thanks for the other comments they did help as well :)
Answer by hav_ngs_ru · Mar 20, 2015 at 11:53 AM
renderer.material.color.a - for gameobjects
GUI.color.a - for GUI elements
and... taadaaaaa! LeanTween.alpha for leanTween
thanks, seems to be working however the effect is not working like I wanted, the button is ALWAYS active. this is my code snippet.
void Start () {
this.GetComponent<Button> ().interactable = false;
fadeIn();
}
void Update() {
if (GUI.color.a == 1f) {
this.GetComponent<Button>().interactable = true;
}
}
I feel like I need to pass in another variable to the if statement but cant work it out.
seems like you use new UI, but trying to get an old GUI.color
Answer by DiegoSLTS · Mar 20, 2015 at 01:07 PM
I've never used LeanTween, but looking at the documentation it has a LeanTween.isTweening method that looks like it would return false once the tween has finished.
Try checking if the tween is still tweening instead of checking the alpha, it makes more sense if you think about it, you just want to know if it finished, and with this method you can change the final alpha value if you need to and everything should still work.
sweet that works fine, however I would like to make the button active just before it maxes 1f alpha so > .9f due to no visual difference between being active or not. but up vote for a working answer but just won't work for me.