How to wait until an animation has played?

I know this question has been asked multiple times here, but I haven’t been able to find a good solution for it. I’m trying to program “Battle City” from the NES on my own, and I’m a total beginner.

Before the main player (the tank) spawns on the screen at the beginning of a game, an animation of a 4-pointed flickering star plays right on the spot where the tank is going to spawn.

You’ll be able to see it in this video: - YouTube

I’ve made a prefab of that star with the animation. So, what I do is I instantiate the prefab and then instantiate the tank. The problem obviously is that the tank is instantiated while the animation of the star is still playing.

I have created a “startGame” GameObject for this purpose. I load the tank prefab and the star prefab on it, and instantiate them through scripting.

Also, where can I find an exhaustive guide to all the concepts and classes of Unity? I’m learning a lot, but every little step is a nightmare…

Edit:

This is the code I have so far. It’s rather simple…

public GameObject player1;
public GameObject spawnEffect;

// Use this for initialization
void Start () {

Instantiate (spawnEffect, transform.position, transform.rotation);
GameObject Tank = Instantiate (player1, transform.position, transform.rotation) as GameObject;
//this here is for the waves
GameObject.FindGameObjectWithTag ("Tank").GetComponent ("spawnWaves").SendMessage ("createWaves");

}

void Start () {
GameObject star = Instantiate (spawnEffect, transform.position, transform.rotation) as GameObject;
star.GetComponent().StartAnimation(); // Need to create that method that just starts the animation
GameObject Tank = Instantiate (player1, transform.position, transform.rotation) as GameObject;
tank.SetActive(false);
StartCoroutine(StarAnim());
}

IEnumerator StarAnim(){
   while(star.animation.IsPlaying("IntroAnimation")){
       yield return null;
   }
   Destroy(star);
   tank.SetActive(true);
}

So, first you create your two objects, you have a script on the star that I called AnimScript but up to you to name it different. It has a method that I called StartAnimation. You could also access the Animation component, or simply trigger the animation on Awake.

The tank is deactivated so that it does not do anything. A coroutine is started and it will run until the animation I named IntroAnimation is running.

Once the star animation is done (make sure it is not looping), then the while loop of the coroutine is done, the star is destroyed, the tank is activated.

You should show us the code you already have, it would be quicker.

What you should already have is something like this :

In function Start :

  • you instantiate the star
  • you play your animation
  • you instantiate the tank

Something else that could work :

In function Start :

  • you instantiate the star
  • you play the animation

In function Update :

  • if the star animation is not playing, instantiate the tank

Make sure to instantiate the tank only once with a boolean for example that you valuate to false by default and to true when you instantiated the tank the first time.

Thanks a lot for your answer. But, if it’s not too much asking, could you sketch out at least what I should do inside AnimScript? The bottom of all this is that I still don’t know my way around the animation class…

Currently, the “star” prefab has an Animator component, but not an Animation component. Is that a problem?

Again, thank you! I really appreciate your help! :smiley:

I think I’m advancing little by little, but I still have a few things to figure out.

I have removed the “Animator” component from the prefab and have attached an “Animation component” with “IntroAnimation”. Now, the code inside “AnimScript” looks like this:

	public Animation anim;



	void destroyMe () {
		Destroy (this.gameObject);
	}

	void StartAnimation () {
		Debug.Log ("gets called");
		anim = this.gameObject.GetComponent("Animation") as Animation; 
		anim.Play ("spawn1anim");
	}

The good news is that, thanks to the code provided by fafase, the tank spawns only after the four-pointed star has gone away. The bad news is that the star is not flickering. It’s stuck on the first frame. What’s going on? I have marked “legacy” in IntroAnimation, so that’s not the problem.

Should I have removed the Animator component from the prefab? When is the Animator Component necessary and when is it not?

Thank you all for your help.