right now in my game, four enemies spawn in four spawn points with a delay, but what I want to do is that if you don't kill the enemy after a couple seconds, an animation (it's an animation of a scratch, basically to let the player know it has been attacked) appears.

Right now I have a script that display an image (it's an image because I was testing the scripts), but now I want to change the image for the animation but after trying and failing a lot of times I decided to ask here, and hopefully someone can help me.

This is the script of the "attack", and it's attached to the enemy prefab:

public Image TargetImage; 

void Start ()
    TargetImage.enabled = false;

public void spawnAttack(){
    StartCoroutine (playerIsAttacked (5, TargetImage));

public IEnumerator playerIsAttacked (float t, Image im)
    yield return new WaitForSeconds (t);
    TargetImage.enabled = true;


and this is in the timed spawn script:

public class TimedSpawn : MonoBehaviour {

public GameObject spawnee;
public bool stopSpawning;
public float spawnTime;
public float spawnDelay;

void Start () {
    InvokeRepeating ("SpawnObject", spawnTime, spawnDelay);

public void SpawnObject (){
    GameObject newSpawn = Instantiate (spawnee, transform.position, transform.rotation) as GameObject;
    newSpawn.GetComponent <enemyAttack> ().spawnAttack ();

    if (stopSpawning) {
        CancelInvoke ("SpawnObject");


  • 1
    \$\begingroup\$ "after trying and failing a lot of times I decided to ask here, and hopefully someone can help me." what is the problem? \$\endgroup\$ Commented Nov 22, 2017 at 4:46

1 Answer 1


You need an Animator on your enemy object as well as an animation controller. You also need to create an idle clip and an attack clip for it, then an animation trigger "isAttacking".

Set the trigger on a timer of some sort, and set up your animation controller to play the attack animation when the trigger is set off.





Here's the docs, otherwise find a simple tutorial on YouTube. It's not as confusing as I made it seem!

  • \$\begingroup\$ Hey Jim, thanks for contributing to the website. Can you elaborate on the isAttacking, perhaps a small example? \$\endgroup\$
    – Sidar
    Commented Nov 23, 2017 at 20:47
  • \$\begingroup\$ There are code samples in the linked articles. Have you tried applying them? What didn't work? The "isAttacking" trigger mentioned in this post is the same sort of trigger explained in the 4th link. \$\endgroup\$
    – Basic
    Commented Mar 20, 2023 at 0:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .