Mathf.lerp in an onClick method

Hello,

I want to fade in text when a button is clicked. How do I do that?

Regards

I would suggest checking the docs regarding the Marf.Lerp method: Unity - Scripting API: Mathf.Lerp

But in any case, here’s an example of an Image fade in method using a Coroutine:

using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class ImageFader : MonoBehaviour
{
    [SerializeField]
    private Image image = null;

    [SerializeField]
    private float speed = 1f;

    private void Start()
    {
        StartCoroutine(FadeIn());
    }

    private IEnumerator FadeIn()
    {
        SetOpacity(0f);

        while (image.color.a < 0.9f)
        {
            SetOpacity(Mathf.Lerp(
                image.color.a,
                1f,
                Time.deltaTime * speed));

            yield return new WaitForEndOfFrame();
        }

        while (image.color.a < 1f)
        {
            SetOpacity(Mathf.MoveTowards(
                image.color.a,
                1f,
                Time.deltaTime / 5f * speed));

            yield return new WaitForEndOfFrame();
        }

        Debug.Log("DONE");
    }

    private void SetOpacity(float opacity)
    {
        image.color = new Color(
            image.color.r,
            image.color.g,
            image.color.b,
            opacity);
    }
}

Here’s a reference regarding Coroutines: Unity - Manual: Coroutines