LeanTween alpha problems

Hello! I’m trying to use LeanTween to tween the alpha of a Game Object and having som trouble. here’s my code:

using UnityEngine;

public class Fade : MonoBehaviour {
	
	public float duration = 1f;
	public float transparent = 0f;
	public float opaque = 1f;
	
	void Start ()
	{
	
	}
	void OnMouseDown ()
	{
		FadeObjectIn ();
	}
	
	void FadeObjectIn ()
	{
		print ("Fading color in");
		
		// Fade GO in from 0 to 1, but it doesn't work?
		LeanTween.alpha(gameObject, opaque, duration);
	}
	
}

Basically, when the player clicks the Gme Object, it’s supposed to fade in from transparent to Opaque and it doesn’t work. If I try to fade in from Opaque to transparent, it works but the speed is super slow, like 20 times slower than it should be. Anyone know what’s up?

LeanTween version: 2.14
Unity Version: 4.3.4f1
Shader: Transparent/Bumped Defuse

I haven’t used LeanTween yet.

But for now, you can try the following code which uses a coroutine:

void OnMouseDown()
    {
        // you might want to make sure at this point that coroutines of the same type will be stopped first
        StartCoroutine(FadeIn(duration)); // duration variable from your code sample
    }

    IEnumerator FadeIn(float timer)
    {
        Color temp = new Color();
        while (renderer.material.color.a < opaque) // opaque variable from your code sample
        {
            temp = renderer.material.color;
            temp.a += Time.deltaTime/timer;
            renderer.material.color = temp;
            yield return null;
        }
        temp.a = 1f; // set it back to exactly 1f
        renderer.material.color = temp;
    }

Hey Roman!

I am not sure what the problem is, I just tried tweening that shader and it seems to be working fine. Check out the AlphaTesting scene from the github page: https://github.com/dentedpixel/LeanTween to see it in action.

Maybe you are calling the tweening code multiple times? I find it’s best to do things on MouseUp, so that you can insure that it is only fired once…

I think the problem might have been that I didn’t know about and therefore didn’t follow the instructions for setting up LeanTween, which are located on github: GitHub - dentedpixel/LeanTween: LeanTween is an efficient animation engine for Unity

The alphaFade was causing me issues where it would stop half way or not even start, so I rewrote it as a coroutine to guarantee it would always work for me:

StartCoroutine(FadeToAlpha(canvasGroup, 0, 1));

 IEnumerator FadeToAlpha(CanvasGroup canvasGroup, float targetAlpha, float fadeTime)
{
        float startingAlpha = canvasGroup.alpha;

        for (float i = 0; i < 1; i+= Time.deltaTime/fadeTime)
        {
            canvasGroup.alpha = Mathf.Lerp(startingAlpha, targetAlpha, i);

            yield return new WaitForFixedUpdate();
        }


        canvasGroup.alpha = targetAlpha;
}