Stopping an animation when at a specific point..

So, a little hard to write a good title for this question but here goes:

I have a very simple animation, where the alpha value of a material goes from 0 to about 0.8. It’s pulsing, you might say. Until the animation is invoked, the value is 0. Also, the animation is set to wrapMode = WrapMode.PingPong.

My problem is this: The animation is playing at the press of a button, and I want it to stop at the press of another button. But, I would really like it to stop when the alpha value returns to 0 and not just turn off at whatever value. This should make it look like it’s just dimming down and then disappearing when the second button is pressed, instead of just abruptly vanishing.

What i have tried so far is a basic if-statement:

if(GameObject.renderer.material.color.a == 0)

The problem here is that as it’s being executed inside OnGUI, the check is only being run once. Not good.

I’ve also tried changing the wrapMode to Once, but all this does is to play the animation forward from its current frame, and then stop at the last frame – which is 0.8 (not invisible as I want).

What I am basically looking for here is not so much scripting advice, but more some ideas for achieving what I want. don’t be shy – fire away…

When the second button is pressed, set a boolean to true. Then in Update(), if this boolean is true and the alpha value of the material is 0, stop the animation and set the boolean to false. Something like that, do you think?

So, maybe like this:

var stopAnim : boolean = false;

function Update () {

	if (renderer.material.color.a == 0 && stopAnim) {
		stopAnim = false;

function OnGUI () {
	if(GUI.Button(settings)) {
		stopAnim = true;

Settings obviously representing your button width, height, etc.

I hope I’ve interpreted your question correctly … don’t be afraid to down vote me if I’m absolutely nowhere near … :smiley:

Hope that helps! Klep

An idea could be to use a boolean like fadeIn.

function Update(){
          Gui apparition}}
    else if(!fadeIn){
          Gui disappears}}}

Now in your Gui function

function OnGUI ()
	if (GUI.Button (Rect (), "PLAY")) 
               fadeIn=!fadeIn;    //change the boolean that will trigger the fade out 
               yield WaitForSecond(1.0f); // Wait a little so that the fade out happens
               Application.LoadLevel (level); //Load level.	

As you asked, this is just the “idea”.

Hope that helps.