- Home /
 
Space Shooter Tutorial - Player Respaner: IEnumerator not executing all code ,
I have completed the Space Shooter tutortioal and currently wanting to impliment a lives system. I'm open to other ways of doing this but so far to avoid re doing the current meachanics I am using IEnumeratore to set the player to active and false. I am using it in the DestroyByContact script.
My goal is to:
Set Player active to false
wait 3 seconds
Set player active to true but with no collider (shot term invincibility)
wait 3 seconds
Set Player collider to true
For some reason the IEnumeratore just does not work and won't trigger the code after the second wait. If I just have log out put I can get through the two wait times without issue but as soon as I bring in the setactive and collider true/false it breaks. There are no errores in the console.
IEnumerator Function:
     IEnumerator RespawnPlayer()
     {
         Debug.Log("Dead");
         player.SetActive(false);
 
 
         yield return new WaitForSeconds(2);
         Debug.Log("respawn");
         player.SetActive(true);
         playerCollider.enabled = false;
 
         yield return new WaitForSeconds(2);
         Debug.Log("Collider On");
         playerCollider.enabled = true;
 
     }
 
               Full Script
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DestroyByContact : MonoBehaviour
 {
     
     public GameObject explosion;
     public GameObject playerExplosion;
     public int scoreValue;
     
     private GameController gameController;
 
     private bool gameOver;
     private GameObject player;
     private Collider playerCollider;
 
 
     void Start()
     {
         
         GameObject gameControllerObject = GameObject.FindWithTag("GameController");
         if(gameControllerObject != null)
         {
             gameController = gameControllerObject.GetComponent<GameController>();
         }
         if(gameController == null)
         {
             Debug.Log("Cannot find 'Gamecontroller' script");
         }
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.CompareTag("Boundary") || other.CompareTag("Enemy"))
         {
             return;
         }
 
         if (explosion != null)
         { 
         Instantiate(explosion, transform.position, transform.rotation);
         }   
 
         if(other.CompareTag("Player"))
         {
             Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
 
             gameOver = gameController.GameOver();
 
             if(gameOver == false)
             {
                 player = GameObject.FindGameObjectWithTag("Player");
                 playerCollider = player.GetComponent<Collider>();
 
                 
                 StartCoroutine(RespawnPlayer());
               
                 return;
             }
         }
         else // the else stops points being scored from cashing into an enemy/hazzard  
         {
             gameController.AddScore(scoreValue);
             
         }
 
         Destroy(other.gameObject);
         Destroy(gameObject); 
     }
 
 
  
     IEnumerator RespawnPlayer()
     {
         Debug.Log("Dead");
         player.SetActive(false);
 
 
         yield return new WaitForSeconds(2);
         Debug.Log("respawn");
         player.SetActive(true);
         playerCollider.enabled = false;
 
         yield return new WaitForSeconds(2);
         Debug.Log("Collider On");
         playerCollider.enabled = true;
 
     }
 
 }
 
               GameOver function from the GameController Script:
 public bool GameOver()
     {
         if(lives > 0)
         {
             lives = lives - 1;
             livesText.text = "X " + lives;
             gameOver = false;
             return false;
         }
         gameOverText.text ="Game Over";
         totalScoreText.text = "Top Score: " + score;
 
         gameOver = true;
         return true;
     }
 
               Am I using the I enumerator in the wrong way?
Thanks
"as soon as I bring in the setactive and collider true/false it break"
what do you mean by it breaks? Nothing happens, game stops, ... ?
Answer by FlaSh-G · Apr 04, 2018 at 12:18 PM
Coroutines are handled by the component they have been started on. They are not stored in some sort of global coroutine register or anything.
Thus, this combination:
 StartCoroutine(RespawnPlayer());
 // ...
 Destroy(gameObject);
 
               means that you start a coroutine, but destroy it alongside the object you started it on before the next frame. As a result, the coroutine will execute anything until the first yield statement, but will be destroyed before it can continue after that.
Your answer
 
             Follow this Question
Related Questions
How to implement a respawn/lives system in Space Shooter (the tutorial game)? 0 Answers
whats wrong.... 0 Answers
In the space shooter tutorial on the "creating shots" the bolt won't move 10 Answers
Space Shooter DestroyByContact Script outputs double the amount of score inputted for asteroid 0 Answers
Space Shooter Shot Issues,Space Shooter Bolt Issues 0 Answers