Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SamuelJM · Apr 03, 2018 at 04:08 AM · respawnspace shooter

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:

  1. Set Player active to false

  2. wait 3 seconds

  3. Set player active to true but with no collider (shot term invincibility)

  4. wait 3 seconds

  5. 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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mgear · Apr 03, 2018 at 06:10 AM 0
Share

"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, ... ?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

77 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges