How to start a Coroutine in another script?

by detdimples   Last Updated November 15, 2017 00:13 AM

I did some research about this but what i found was kinda confusing so I didn't actually understand it. Basically I have a coroutine that displays an image after 5 seconds, but I want that image to display 5 seconds after an enemy spawns. I understand that what I need to do is call StartCoroutine in my enemy spawning function, but I'm very new to programming and i couldn't do it, even though it's probably something really easy.

Here's the code that displays the image:

public class enemyAttack : MonoBehaviour {

 public Image TargetImage; 

 void Start ()
     TargetImage.enabled = false;
     StartCoroutine(playerIsAttacked (5, TargetImage));


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


and this is the code that spawns the enemies:

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 (){
     Instantiate (spawnee, transform.position, transform.rotation);

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


Related Questions

NullReferenceException in StartCoroutine method

Updated November 25, 2016 08:05 AM