# Can't attach animation to script

I'm working on a little game (which is also my first one) and I want to insert a splash screen at the beginning (after the Unity one). I've finally achieved to add a fade-in and a fade-out transistion when the splash screen starts and ends, but the fade out is activated by a button and I wont to play it simply after a certain time. I've made a script for that:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

{
public Animation anim;

IEnumerator Start()
{
yield return new WaitForSeconds(2f);

anim.GetComponent<Animation>().Play();

}

}


The problem is: when I try to attach the animation the Inspector refuses to do it.

Any ideas? Thanks

• Why are you calling GetComponent on anim, when anim is an Animation component? – jhocking Sep 10 '15 at 1:31
• @jhocking is right. You have already assigned a clip to anim, so there is no need to call GetComponent. Just say anim.Play(); – SanSolo Dec 9 '15 at 5:43
• just another bit of info: you can use transform.GetComponent<COMPONENT>() to access another component on the same object. – Nika Kasradze Dec 9 '15 at 11:49

You aren't waiting on the animation so once you play it Application.LoadLevel just does its thing and you wont notice the animation.

First, you'll need a way to wait on the animation which could be a simple coroutine:

private IEnumerator WaitForAnimation ( Animation animation )
{
do
{
yield return null;
} while ( animation.isPlaying );
}


And then use it to yield before calling Application.LoadLevel:

    ...
anim.Play();
yield WaitForAnimation(anim);


Also, you don't need to call GetComponent<Animation> on an Animation.
• The Start() function of a monobehaviour can be used as a coroutine without calling the StartCoroutine(). – Lasse Oct 10 '15 at 7:01