0
\$\begingroup\$

I have a brain model. I want to glow the parts of the brain model one after the another after a period of time. I have done to glow the parts, but when the second part glows, the color of the first part still remains.

public GameObject pons;
public GameObject frontallobe1;

void Update () {
    Invoke("PonsGlow",2);
    Invoke("frontalLobe1Glow",4);
}
void PonsGlow()
{
    pons.renderer.material.color=Color.red;
}
void frontalLobe1Glow()
{
    frontallobe1.renderer.material.color=Color.red;
}
\$\endgroup\$
6
  • \$\begingroup\$ Please describe, how exactly do you want the parts of the brain to glow one after the another? \$\endgroup\$
    – Kromster
    Commented Jan 14, 2015 at 6:08
  • 1
    \$\begingroup\$ Am I over-simplifying by suggesting that you simply need to set the first objects color from red back to its initial color? \$\endgroup\$
    – HeavyE
    Commented Jan 14, 2015 at 6:33
  • \$\begingroup\$ @Krom Stern : its should glow one after another,I mean when the first color of the color change after 2 sec,then when the 2nd parts should change to red ,along with that the first part color should change back to its initial color \$\endgroup\$ Commented Jan 14, 2015 at 7:23
  • \$\begingroup\$ @HeavyE : Suppose my brain parts color is orange at initial,from the code after 2 sec the first part change the color to red,after 4 sec the second part should change to red color along wit that the first part color should change to its intital color(oranage) \$\endgroup\$ Commented Jan 14, 2015 at 7:25
  • \$\begingroup\$ Would InvokeRepeat work for you? \$\endgroup\$
    – Kromster
    Commented Jan 14, 2015 at 8:11

1 Answer 1

1
\$\begingroup\$
public GameObject pons;
public GameObject frontallobe1;

void Start() {//runs when this gameobject is first added to the game and only runs once.
    StartCoroutine("Glow", 2f, pons);
    StartCoroutine("Glow", 4f, frontallobe1);
}

//store the last glowing object and color,
//so it can stop glowing.
private GameObject lastGlow;
private Color preGlowColor;

IEnumerator Glow(float waitTime, GameObject go){
    yield return new WaitForSeconds(waitTime);

    if(lastGlow!=null){//if first time running, ignore this
        lastGlow.renderer.material.color=preGlowColor;//set the color back to its original color
    }
    preGlowColor = go.renderer.material.color;//save the original color
    lastGlow = go;//save the now glowing game object, replacing any prior ones
    go.renderer.material.color=Color.red;//make gameobject glow - you might even want to make a fancy Color.Lerp here
}

There are many ways to skin this cat. The above way works by 'saving' the last glowing thing (and it's color, if that's important) so that when something new glows, it 'cancels' the old glow by replacing the last object 'glow color' with its original color. But this way will only work if only one thing is glowing at a time (if you want to have two things glowing, the first one will be canceled by the second one).

If the requirement is that 'any number of things can start glowing at the same time', then you will need to make an array (a big list of stuff) of all the things that are currently glowing (and possibly their original colors as well), so that when something new starts to glow that all the old stuff (everything in the list of 'currently glowing' stuff) gets turned off properly.

Also adding a color lerp in there would make it look pretty, instead of just 'popping' on and off.

And lastly, if you didn't notice, I changed the invoke to a start coroutine because I think it made it look a little prettier (or more readable to me). Also I changed the Update function to Start: this means that it is only ran once, rather than once per rendered frame (if it was still in update, it would flicker between the two pieces over and over again, after about 4 seconds).

Hope that helps!

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .