Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Peresef · Mar 11, 2015 at 09:15 AM · coroutineyield waitforseconds

C# yield waitforseconds

Hey guys, I started programing a game and i'm currently working on the respawn function using checkpoints. So far everything is going well except one little detail. I'm using a coroutine to make the player wait for X time when he dies and then call the respawn function. The real problem is, everything is executed perfectly until the corutine passes the X seconds wait.

What i want to accomplish is that the player loses control for X seconds and then the 2nd block from the coroutine spawns the player ONCE. I really don't know why, but after the wait the coroutine keeps executing the 2nd block X seconds.

I would appreciate if anyone could give me a tip on why this is happening, i'm new using the coroutines so i might have fucked it up somewhere.

 public void KillPlayer(){
         StartCoroutine (KillPlayerCo ());
     }
 
     private IEnumerator KillPlayerCo(){
         Seguimentcamara go = GameObject.Find ("Camera").GetComponent<Seguimentcamara> ();
         Player pl = _pc.GetComponent<Player> ();
         pl.Kill ();
         go.isfollowing = false;
 
         yield return new WaitForSeconds (2f);
         Debug.Log ("WE HAVE WAITED 2S");
         go.isfollowing = true;
         if (_currentCheckpointIndex != -1)
             _checkpoints [_currentCheckpointIndex].SpawnPlayer(_checkpoints[_currentCheckpointIndex]);
         Debug.Log ("END");
     }

I've also tried to add a yield break; at the end and it keeps looping the second block.

I've done a few tests with the Debug.Log and so far the flow of the code works like this:

 public void KillPlayer(){ 
           /*When Killplayer is called it executes this once and then it starts the 
           coroutine.*/
           StartCoroutine (KillPlayerCo ());
      }
  
      private IEnumerator KillPlayerCo(){
          //This block keeps looping until 2s have happened
          yield return new WaitForSeconds (2f);
          //This block keeps looping until 2s have happened
      }

Is this how it should happen? if so how can i code it so it only executes the second part once? I've also tried adding a flag inside the second code that calls the respawn function when the flag is true, after executing the respawn it changes to false. The only way to change it back to true is going through the call to start the coroutine. YET it keeps calling the respawn function even with the flag set to false.

PD: Added a bit more info after some tests.

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 Peresef · Mar 10, 2015 at 10:14 PM 0
Share

Yup, I've tried multiple Debug.Logs to keep track of the flow and after 2s it only repeats the second block.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by CorruptedTNC · Mar 11, 2015 at 11:55 AM

The only explanation is that you're calling the co-routine multiple times without meaning to. The following code works just fine for me:

 using UnityEngine;
 using System.Collections;
 
 public class Testing : MonoBehaviour {
 
     void OnGUI()
     {
         if(GUILayout.Button("Testing"))
         {
             StartCoroutine(test ());
         }
     }
 
     private IEnumerator test()
     {
         Debug.Log("Start");
         yield return new WaitForSeconds (2f);
         Debug.Log("End");
     }
 }
 

Have a look for when the co-routine is being called, is it when the player collides with something? Health reaches 0 or below? The conditions for calling the respawn code is likely executing more than you'd want it to, as co-routines don't loop without being told to.

Comment
Add comment · Show 2 · 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 Peresef · Mar 11, 2015 at 02:55 PM 0
Share

You were absolutly right man, when i was calling the coroutine from the health script it took sometime for it to actualize the curhealth and it kept calling the coroutine every frame until the first one reached the point were it changed the value. I guess yesterday i was too tired to notice it. Thank you!

avatar image CorruptedTNC · Mar 15, 2015 at 11:15 AM 0
Share

Glad to help, and glad you solved the problem :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

IEnumerator not looping correctly? 1 Answer

Coroutine yield return new Waitforseconds not working 1 Answer

yield return new WaitForSeconds () not working 1 Answer

How to pause a Coroutine? 5 Answers

Coroutines Didn't Work After Changing Scene 1 Answer


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