MathF.Lerp not lerping?

So I have this script that when a button is clicked, the text sprite on a gameobject (who’s alpha value starts at 0) lerps up to full visibility:

    public float FadeSpeed = 0.02f;
	public bool FadingIn = false;
	public GameObject AlreadyBoughtText;
	public Color TempColor;

void Awake ()
{
   TempColor.a = 0;
   AlreadyBoughtText.renderer.material.color = TempColor;
}

void Update ()
{
   if (FadingIn == true)
	FadeIn();
}

void FadeIn()
	{
		TempColor = AlreadyBoughtText.renderer.material.color;
		TempColor.a = Mathf.Lerp(0, 1, FadeSpeed);
		TempColor.g = Mathf.Lerp(0, 1, FadeSpeed);
		TempColor.b = Mathf.Lerp(0, 1, FadeSpeed);
		TempColor.r = Mathf.Lerp(0, 1, FadeSpeed);
		AlreadyBoughtText.renderer.material.color = TempColor;
		Debug.Log(AlreadyBoughtText.renderer.material.color.a);

		if (TempColor.a == 1 && TempColor.g == 1 && TempColor.b == 1 &&     TempColor.r == 1)
		{
			FadingIn = false;
			//FadeOut();
		}
	}

For some reason though, the MathF.Lerp doesn’t want to gradually raise to 1, instead the second the method is called, it is instantly 1. Am I doing something wrong…?

Your error consists that you not correctly use Mathf.Lerp() speed. You accepted speed equal 0.02, but for example if you have 60 FPS, runtime have slightly less than a second. Change your value of speed and the Lerp function to values which I wrote below:

 public float FadeSpeed = 0.2f; //change value on 0.2 per second
 ...
 void FadeIn() {
  TempColor = AlreadyBoughtText.renderer.material.color;
  TempColor.a = Mathf.Lerp(0.0f, 1.0f, FadeSpeed*Time.deltaTime);
  TempColor.g = Mathf.Lerp(0.0f, 1.0f, FadeSpeed*Time.deltaTime);
  TempColor.b = Mathf.Lerp(0.0f, 1.0f, FadeSpeed*Time.deltaTime);
  TempColor.r = Mathf.Lerp(0.0f, 1.0f, FadeSpeed*Time.deltaTime);

  if (TempColor.a <= 0.95 && TempColor.g <= 0.95 && TempColor.b <= 0.95 && TempColor.r <= 0.95) {
   TempColor.a = 1.0f;
   TempColor.g = 1.0f;
   TempColor.b = 1.0f;
   TempColor.r = 1.0f;
   FadingIn = false;
  }
  AlreadyBoughtText.renderer.material.color = TempColor;
  Debug.Log(AlreadyBoughtText.renderer.material.color.a); 
 }

Unity - Manual: Coroutines is your solution :slight_smile:

I actually managed to find out how MathF.Lerp in between posting this.

Update ()
{
   if (FadingIn == true)
   {
       FadeSpeed += Time.deltaTime * speed;
       FadeIn();
   }

   if (FadingOut == true)
   {
	FadeSpeed -= Time.deltaTime * speed;
	FadeOut();
   }
}

void FadeIn()
	{
		Debug.Log(FadeSpeed);
		TempColor = AlreadyBoughtText.renderer.material.color;
		TempColor.a = Mathf.MoveTowards(0, 1, FadeSpeed);
		TempColor.g = Mathf.MoveTowards(0, 1, FadeSpeed);
		TempColor.b = Mathf.MoveTowards(0, 1, FadeSpeed);
		TempColor.r = Mathf.MoveTowards(0, 1, FadeSpeed);
		AlreadyBoughtText.renderer.material.color = TempColor;

		if(FadeSpeed >= 1)
		{
			FadingOut = true;
			FadingIn = false;
		}
	}

	void FadeOut()
	{
		TempColor = AlreadyBoughtText.renderer.material.color;
		TempColor.a = Mathf.MoveTowards(0, 1, FadeSpeed);
		TempColor.g = Mathf.MoveTowards(0, 1, FadeSpeed);
		TempColor.b = Mathf.MoveTowards(0, 1, FadeSpeed);
		TempColor.r = Mathf.MoveTowards(0, 1, FadeSpeed);
		AlreadyBoughtText.renderer.material.color = TempColor;

		if(FadeSpeed <= 0)
		{
			FadingOut = false;
		}

	}

Its weird how it works, but apparently your speed variable only reaches the to and from variables from a transition between 0 and 1, 0 being the from state and 1 being the to state. So if you make your speed variable transition from 0 to 1 like in the FadeIn Method, the object will slowly change its alpha from 0 to 1, because the speed going toward 1 means its transitioning toward your “to” state. If that makes sense.