-2
\$\begingroup\$

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");
     }
 }

}

\$\endgroup\$
  • 7
    \$\begingroup\$ Hi @detdimples -- You've asked 6 questions recently on very similar topics. Asking questions isn't an issue in itself, but from your pattern of questions I think you will develop competency much better in the long run by taking a step back from your immediate problem and working through some introductory lessons and tutorials on C# programming and Unity. \$\endgroup\$ – Jimmy Nov 15 '17 at 0:20
1
\$\begingroup\$

Be careful when running coroutines in this manner. Especially lots of them. I agree with user @Jimmy, who left a comment on your question; however, for the sake of pointing you in the right direction and helping you become better at understanding programming and calling functions, I'll detail a solution below.

On your enemyAttack script, add the following code as a function.

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

Then, change your TimedSpawn script, to this:

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");
      }
   }
 }

What's happening here, is that you're creating a function (spawnAttack) within your enemyAttack script which all it does is start the coroutine on the same script. However, because your spawnAttack function is public, it can be accessed outside of this script via referencing.

So in your TimedSpawn script, all you have to do is when you instantiate your new object, you set it to a GameObject within the scope of the SpawnObject function. You then access that GameObject and call the function belonging to it, which happens to be spawnAttack. The spawnAttack function will run, and start your coroutine.

\$\endgroup\$
  • \$\begingroup\$ I understand, but it gives me this error in the console: No overload for method playerIsAttacked' takes 0' arguments. Ps: the reason why I ask questions is because I have a deadline and I need to figure this out, and when I'm done with that I will take the time to watch tutorials and lessons and learn everything better. And thanks for your help \$\endgroup\$ – detdimples Nov 15 '17 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.