0
\$\begingroup\$
public IEnumerator FadeTextmeshproIn(float t, TextMeshProUGUI text)
{
    text.color = new Color(text.color.r, text.color.g, text.color.b, 0);
    TMP_SpriteAsset spriteAsset = text.spriteAsset;
    spriteAsset.material.color = new Color(spriteAsset.material.color.r, spriteAsset.material.color.g, spriteAsset.material.color.b, 0); 

    while (text.color.a < 1.0f)
    {
        text.color = new Color(text.color.r, text.color.g, text.color.b, text.color.a + (Time.deltaTime / t));
        spriteAsset.material.color = new Color(spriteAsset.material.color.r, spriteAsset.material.color.g, spriteAsset.material.color.b, spriteAsset.material.color.a + (Time.deltaTime / t));
        yield return null;
    }
}

This is a function I am using to fade-in a TextMeshPro component. The TextMeshPro is composed of a sprite and a text, as shown in the images below.

However, when the function is called, only the text fades in, and the sprite never appears, as shown in the gif below. I can't seem to find a way to make them both fade in together.

Also note that I would like to keep them in the same component, because splitting them up into two components would make it hard to keep the spacing between them the same as the score next to it gets higher.

TextMeshPro text

TextMeshPro component

No sprite fade-in

\$\endgroup\$
1
  • \$\begingroup\$ Regarding the spacing and two components, that can be done by just having the text left aligned. Are you sure that your highs ore method is not just deleting the sprite reference and you simply just display the score? To make sure, add your highscore setter code as well \$\endgroup\$
    – Zibelas
    Dec 24, 2023 at 13:03

1 Answer 1

0
\$\begingroup\$

You can try to use Image color instead of changing material color (all elements using the same material will fade) maybe you set color alpha of the same material to zero in the else code thats why it never fade in. Just use

UnityEngine.UI.Image img;

and set img.color= new Color(r,g,b,a) instead. It will set color of selected image only.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .