Gaming Gecko
Gaming Gecko

Reputation: 53

Unity Animation playing from coroutine

As the title states the animation is not playing. the line telling it to play is in a coroutine and the code is before a waitforseconds(3f).

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Play : MonoBehaviour {

public Animator AnimatorRef;
// Use this for initialization
void Start () {
    if (AnimatorRef == null)
    {
        AnimatorRef = GetComponent<Animator>();
    }

}

public void PlayGame()
{
    StartCoroutine(TitlePlay());
    Debug.Log("playing");
}

IEnumerator TitlePlay()
{
    Debug.Log("playing1");
    AnimatorRef.SetBool("Enlarge", true);
    yield return new WaitForSeconds(3f);
    Debug.Log("playing2");

    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}

}

it grabs the animator reference fine and all three of the comments show.

Upvotes: 1

Views: 3220

Answers (2)

Gaming Gecko
Gaming Gecko

Reputation: 53

My unity shutdown without saving but it now works. If anyone has this problem remake the animation and MAKE SURE you go from the initial animation and click add clip. Then it worked for me :)

Upvotes: 0

Doh09
Doh09

Reputation: 2385

2 Considerations.

  • 1st -

Did you check your transitions and AnimationController?

You can open up the AnimationController to see if the bool changes during runtime, if it does you'll know there is a transition error somewhere between your animation states.

  • 2nd -

If you comment out the "LoadScene" part, does the animation then play correctly?

I suspect that the Animation bool is for some reason not allowed to carry out it's actions before the entire method has been run through, could be wrong though.

Upvotes: 1

Related Questions