Hello, I am trying to fade in a gameObject but my script here doesnt seem to work when I call the coroutine.

IEnumerator FadeTo(float targetOpacity, float duration)
{
Material material = gameObject.GetComponent().material;

    Color color = material.color;

    float startOpacity = color.a;

    float time = 0;

    while (time < duration)
    {
        time += Time.deltaTime;

        float blend = Mathf.Clamp01(time / duration);

        color.a = Mathf.Lerp(startOpacity, targetOpacity, blend);

        gameObject.GetComponent<MeshRenderer>().material.color = color;
    }

    yield return null;
}

check your shader type, if it is using alpha or not, like if you want to use alpha, you can use standard shader and set the settings to “Fade” mode rather than “Opaque”. if you already did these settings then check your code if you are calling the coroutine correctly or not? i mean have you used the startcoroutine(Fadeto()), or not?