- Home /
Fade TextMesh font C#
Hello all,
I'm trying to fade the font of a TextMesh, i can change color font dynamically, but the not the alpa. Here's my code.
public class FadeTextUp : MonoBehaviour {
public Color TextColor = Color.blue;
void Awake() { } // Use this for initialization void Start () { gameObject.renderer.material.color = TextColor; gameObject.renderer.material.color.a = TextColor.a; //error line }
// Update is called once per frame void Update () { gameObject.transform.Translate(0, 0.1f, 0, Space.World); //Here i would put the code for fadding the font ... }
}
But i'm getting this error : Cannot modify a value type return value of `UnityEngine.Material.color'. Consider storing the value in a temporary variable.
What should I do ? Thanks
Answer by CHPedersen · May 13, 2011 at 09:26 AM
That, or you can grab the material of the MeshRenderer that every TextMesh comes with like so:
GetComponent<MeshRenderer>().material.color = new Color(TextColor.r, TextColor.g, TextColor.b, alpha);
That's what I'm doing. To achieve a fading effect I then Mathf.lerp the alpha value.
Answer by lampshade · May 13, 2011 at 09:18 AM
That error is just like a read only, you cannot modify it. What you want is to use the function SetColor and specifiy what you are trying to do as its first string parameter. For example:
transform.renderer.material.SetColor("_Color", new Color(renderer.material.color.r,renderer.material.color.g,renderer.material.color.b, renderer.material.color.a - 0.75f));
Notice that I am using transform which indicates that i want to change the color of the material the script is attached to.
You can still use gameObject in place of transform as your code shows.
Let me know if that does not do at least something.
Answer by 1Mikhael · Jan 13, 2016 at 10:31 AM
Hi! What about
var comText = GameObject.FindGameObjectWithTag("comText");
in Update:
if(timer >= 0)
{
comText.GetComponent.<TextMesh>().color.a += 0.4 * Time.deltaTime;
}
Answer by Zarax · May 13, 2011 at 09:28 AM
Well thanks for your response, I've found a solution at my problem.
public class FadeTextUp : MonoBehaviour {
public Color TextColor = Color.blue; void Awake() { } // Use this for initialization void Start () { gameObject.renderer.material.color = TextColor;
}
// Update is called once per frame void Update () { gameObject.transform.Translate(0, 0.1f, 0, Space.World); TextColor.a = TextColor.a-0.1f; gameObject.renderer.material.color = TextColor;
}
}
Now, it works.
I call that Pulling Out a variable. Works for most "parts are Read-Only" problems. For example transform.position.x++;
is a read-only error, so pull it out: Vector3 POS = tr.pos; POS.x++; tr.pos=POS;
. The posts above solve it by setting all parts at once, which is really the same thing, compressed into one step.