x


Activate Animation => Wait => Hide Gameobject

Hi there! Im developing with Vuforia AR plugin. I want to Activate an animated gameobject, wait for some seconds and the hide that gameobject and show another one! I tried differents things(invoke, WaitForSeconds, Destroy(GO,time)<= this one runned ok but when I tried to show the GO again obviusly I was in trouble) but I can't make it run the delay. The code:

public IEnumerator Delay () 
{
    Debug.Log("Funcion delay");
    yield return new WaitForSeconds(0.1f);
}

void Update() 
{
    if( TouchEvent ) // PSEUDO CODE
    {
        // ** RELEVANT PART **
        menu2.renderer.enabled = false;
        GameObject animacion = GameObject.Find("RULETA");
        activarAnimacion(animacion);

        desactivarAnimacion(animacion);
        menu2.renderer.enabled = true;
        // ** RELEVANT PART **
    }
}

void activarAnimacion(GameObject animacion)
{
    // [...]
    //activo la animacion
    animacion.animation.Play();
    Delay();
}
void desactivarAnimacion(GameObject animacion)
{
    // [...]
    //desactivo la animacion
    animacion.animation.Stop();
    Debug.Log("Desactiva anim");
}

Thank you!

[ Edit by berenger : trimmed irrelevant code. Full code available at http://pastebin.com/nA3SXHRt]

more ▼

asked May 03, 2012 at 10:28 PM

PasZ gravatar image

PasZ
1 6 4 4

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

So, you are not using coroutines correctly.

First, a coroutine will only delay the execution of the code following the yield inside its scope. Wich means that your Delay function exectute the Debug.Log, wait and leave. But Update didn't wait. So the animation was started the stopped at the same frame.

Secondly, in C# you need to call a coroutine with StartCoroutine, or it won't be executed at all.

You can't use Invoke directly because you have a parameter. You could store the animacion variable into a member though. Anyway, for the coroutine. When you have the event triggering the animation, use StartCoroutine( Animate(animacion) ); Here is Animate's code :

private IEnumerator Animate( GameObject A )
{
    // ...
    activarAnimacion( A );

    // Unless you're sure the animation's length is 0.1 seconds,
    // you should use A.animation[ A.animation.clip.name ].Length here
    yield return new WaitForSeconds(0.1f);

    // ...
    desactivarAnimacion( A );
}
more ▼

answered May 03, 2012 at 10:50 PM

Berenger gravatar image

Berenger
12.8k 39 42 87

Thanks a lot for this answer, I've been 4 days searching for something like that :)

Aug 27, 2012 at 01:09 AM Kehos
(comments are locked)
10|3000 characters needed characters left

Hey dear! Lot of thanks for your answer! I tried compiling with this code: http://pastebin.com/NV2aLt5A

But nothing was rendered! So I used debugging logs and I checked that the Animate function never is executed! Any idea??

PasZ

more ▼

answered May 04, 2012 at 07:36 PM

PasZ gravatar image

PasZ
1 6 4 4

Try" target="_blank">http://pastebin.com/bGaLiTsx">Try this one. I changed the start function, the wait length and the inside of the switch

May 04, 2012 at 08:10 PM Berenger
(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:

x788
x277
x266
x91

asked: May 03, 2012 at 10:28 PM

Seen: 2171 times

Last Updated: Aug 27, 2012 at 01:09 AM