Editing the alpha of one GUItext edits the alpha of them all

I’ve finally succeeded in getting my text to fade in and then back out. The problem is that now all my GUItext’s, not just the one that I specify, end up having their alpha changed:

#pragma strict
var thoughtText : GUIText;
var fadeIn :boolean;
var fadeOut :boolean;
var shedSeen :boolean;
 
function Start () {
        thoughtText.font.material.color.a = 0;
        fadeIn = false;
        fadeOut = false;
        shedSeen = false;
}
 
function OnTriggerEnter (other : Collider) {
        if (other.gameObject.name == "Shed Thoughts"){
                if (shedSeen == false) {
                        thoughtText.text = "Oh lord";
                        Debug.Log("Fade in");
                        fadeIn = true;
                        yield WaitForSeconds (5);
                        Debug.Log("Fade out");
                        fadeOut = true;
                        shedSeen = true;
                }
        }
}
       
function OnTriggerExit (other : Collider) {
        thoughtText.text = "";
}
 
function FadeIn(){
   if(thoughtText.font.material.color.a<1)
      thoughtText.font.material.color.a += Time.deltaTime;
   else fadeIn = false;
}
 
function FadeOut(){
   if(thoughtText.font.material.color.a >= 0)
      thoughtText.font.material.color.a -= Time.deltaTime;
   else fadeOut = false;
}
 
function Update(){
   if(fadeIn)FadeIn ();
   if(fadeOut)FadeOut ();
}

Any help would be greatly appreciated.

I've pasted the code in your question in order to make it easier to read and answer.

1 Answer

1

Use material.color instead of font.material.color:

...
function FadeIn(){
   if(thoughtText.material.color.a<1)
      thoughtText.material.color.a += Time.deltaTime;
   else fadeIn = false;
}
 
function FadeOut(){
   if(thoughtText.material.color.a >= 0)
      thoughtText.material.color.a -= Time.deltaTime;
   else fadeOut = false;
}
...

Thank you, that has fixed my problem . . . though I'm very confused as to WHY it fixed my problem.

I suppose that guiText.material is a copy of guiText.font.material, thus modifying it doesn't affect other GUIText entities - but changes in guiText.font.material are transferred to the copies.

It would be good to look into the difference between "material" and "shared material", when they are created and how they effect your project!