- Home /
Text not reverting back to original properties after gameplay
I have no idea why this is happening but for some reason, after changing the alpha value to zero of the text material colour during gameplay, the alpha value does not revert back after I click the stop button. The alpha value remains 0. Here is the code i am doing:
IEnumerator Fade(){
for (float alpha = 1; alpha > 0; alpha -= (Time.deltaTime * fadeSpeed)) {
Color color = gameObject.GetComponent<Text>().material.color;
color.a = alpha;
gameObject.GetComponent<Text>().material.color = color;
yield return null;
}
}
Any suggestions on why this is happening?
Answer by zach-r-d · Jun 25, 2015 at 09:59 PM
This is probably because, unlike MeshRenderer.material, Text.material does not make a copy of the material before editing it. So if the material of the Text component is an asset, changing its properties in play mode will edit the asset directly rather than a copy that will cease to exist when play mode is exited.
One quick solution is to manually make a copy of the material when in the editor; try sticking this in the Start() method of the same script:
#if UNITY_EDITOR
GetComponent<Text>().material = new Material(GetComponent<Text>().material);
#endif
Thanks, I was wondering for quite some time of why this was happening. Btw can you think of any particular reason, why text material doesn't make a copy before editing?
Probably to prevent leaks. When cloning a $$anonymous$$aterial, it is the script's responsibility to Destroy() it. See the note in the documentation for Renderer.material.
Your answer
Follow this Question
Related Questions
Changing two different objects renderer colour 1 Answer
Material doesn't have a color property '_Color' 4 Answers
Cannot change material colour 2 Answers
How do I get the current alpha value of a material? 2 Answers
How to change font color 4 Answers