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
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.
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.
Hi! What about
var comText = GameObject.FindGameObjectWithTag("comText");
in Update:
if(timer >= 0)
{
comText.GetComponent.<TextMesh>().color.a += 0.4 * Time.deltaTime;
}
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.