- Home /
 
Invoke/Coroutine problems with events
Here is my code. OnPlayerDeath is an event from a different class. I tried calling it this way, and as a part of a coroutine where it would wait for 1 second then call. In both cases, the sound plays and then nothing happens. I also tried calling the event alone without it being a part of a different method and still nothing. Please let me know if anyone can help!
 void OnTriggerEnter2D(Collider2D triggerCollider){
     if (triggerCollider.tag == "Spike") {
         for (int i = 0; i < 1; i++) {
             if (OnPlayerDeath != null) {
                 timeToCompleteLaps = LapComplete.totalTime;
                 Player.moveSpeed = moveSpeed;
                 Destroy (gameObject);
                 Explosion.Play ();
                         Invoke ("PlayerDeath", 1);
             }
         }
     }
 }
 void PlayerDeath(){
     OnPlayerDeath ();
 }
 
              Answer by Jinkata · Jul 10, 2017 at 10:23 PM
I'm not sure if Invoke works if you've already destroyed the GameObject. You could move the Destroy call to the PlayerDeath. Also, if OnPlayerDeathis in a different class how are you calling it without a preceding variable or class name (in the case of a static method)? 
Answer by Jwizard93 · Jul 10, 2017 at 10:30 PM
A coroutine can't have return type void. Any coroutine should be declared:
 IEnumerator NameOfCoroutine( variables )
 {
     // the coroutine
 }
 
              Your answer