• 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 /
This question was closed Jan 25, 2015 at 08:31 PM by meat5000 for the following reason:

The question is answered

avatar image
0
Question by Stiz · Jan 24, 2015 at 01:45 AM · delayrespawn

Respawn Delay Not Working [Closed]

Hi,

I'm playing around with the "Space Shooter" project and am having trouble with Player respawns. Basically, what I want to do is respawn the player after a small delay using the WaitForSeconds function. Everything's working fine except the delay and I have no idea why.

Here's the code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class DestroyByContact : MonoBehaviour
 {
     public GameObject explosion;
     public GameObject playerExplosion;
     public int scoreValue;
     private GameController gameController;
     
     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.tag == "Boundary")
         {
             return;
         }
 
         Instantiate(explosion, transform.position, transform.rotation);
 
         if (other.tag == "Player")
         {
             Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
             gameController.lives -= 1;
             if (gameController.lives == 0)
             {
                 gameController.GameOver ();
             }
             else
             {
                 StartCoroutine (PlayerSpawnWait ());
                 gameController.PlayerSpawn();
             }
         }
     
         gameController.AddScore (scoreValue);
         Destroy(other.gameObject);
         Destroy(gameObject);
     }
     public IEnumerator PlayerSpawnWait()
     
     {
         yield return new WaitForSeconds(2);
         Debug.Log ("Waiting...");
     }
 }

All this gets me is an instant respawn. I've tried putting the PlayerSpawn function in the Coroutine and still no dice. Interestingly, if I put the Debug message in the PlayerSpawnWait coroutine above the WaitForSeconds function it shows up in the console, so clearly the coroutine is being called... it's just the delay isn't working.

Any ideas? Help much appreciated. :)

Comment
Add comment
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

2 Replies

  • Sort: 
avatar image
1

Answer by carrollh · Jan 24, 2015 at 02:07 AM

Try putting line 45 just after your yield statement in PlayerSpawnWait() instead of just after the call to StartCoroutine().

EDIT: disregard the rest of this, it's just me being dumb. Read the other answer about how to "lock a thread" :P

Coroutines are kind of like threads. And the only thing you are doing in yours currently is waiting. While the "main thread" continues on like nothing happened. Which is why you get an instant spawn. Moving your spawn line into the coroutine "thread" should sort it out.

(I don't think it's really a thread. C# uses Tasks anyway, but I'm not really solid on it so I don't want to mislead you at all. But "works" like a thread, and that's good enough I think.)

Comment
Add comment · Show 4 · 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
avatar image Stiz · Jan 24, 2015 at 10:04 AM 0
Share

Update: managed to solve it. $$anonymous$$oved the WaitForSeconds() function over to the Game Controller and called it from the DestroyByObject script. Well, actually, it wouldn't let me call the IEnumerator directly, so I had to call a function to call the IEnumerator.

In any case: it's fixed now. Yay. Thanks again for your help. :)

avatar image KillMobil · Jan 24, 2015 at 04:22 PM 0
Share

you may have solved the problem but did understand the problem? I suggest you give a go to the solution above to see how you could solve this for future reference!

avatar image Stiz · Jan 25, 2015 at 12:11 AM 0
Share

I'd actually tried carrollh's approach before posting the original question but forgot to mention it. Not sure why it didn't work, but yeah: no dice.

avatar image LeonardNS · Jan 25, 2015 at 01:05 PM 0
Share

If you solved it you should close the case.

avatar image
1

Answer by meat5000 · Jan 25, 2015 at 11:24 AM

A running coroutine will halt a main thread. The main code will not carry on regardless. You can test this by putting a infinite loop in your coroutine and watching your program grind to a halt.

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Fade on death! 1 Answer

2D project - player respawn delay problem 0 Answers

How to make my espawn delay trigger to work more than once. 2 Answers

How to make an enemy respawn? 0 Answers

Respawning a player after a certain amount of time 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges