x


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.

more ▼

asked Aug 04 '12 at 10:51 PM

Fistch gravatar image

Fistch
20 3 5

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

Aug 05 '12 at 12:07 AM aldonaletto
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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;
}
...
more ▼

answered Aug 05 '12 at 12:11 AM

aldonaletto gravatar image

aldonaletto
41.2k 16 42 195

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

Aug 05 '12 at 08:22 AM Fistch

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.

Aug 05 '12 at 04:49 PM aldonaletto

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

Aug 05 '12 at 07:39 PM Little Angel ♦♦
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x807
x326
x313

asked: Aug 04 '12 at 10:51 PM

Seen: 495 times

Last Updated: Aug 05 '12 at 07:39 PM