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 /
  • Help Room /
avatar image
0
Question by shrey150 · Nov 02, 2015 at 06:29 PM · c#networkingcoroutinewaitforsecondscoroutine errors

WaitForSeconds not working in C#

I am making an FPS that's set on the cellular level with pathogens and the immune system, and instead of shooting bullets, the immunities shoot antibodies. I've set up my code so that if a player is hit, they show their damage by changing color to red momentarily, then back to their original color. I've tried to implement a coroutine and call a WaitForSeconds from there, but it's still not working. I've read through articles for at least an hour, yet I can't find why the delay doesn't occur. If you know why this is happening, please tell me. Thanks in advance!

         public void DealDamage(GameObject target,int amount)
         {
         //deal the damage
         this.health -= amount;

         //make the player change color to red
         target.GetComponent<Renderer>().material.color = Color.red;

         //***THIS IS WHERE COROUTINE IS STARTED***
         //wait for 0.5 secs
         StartCoroutine(Wait(0.5f));

         //switch back to original color
         if (target.tag == "Immunity")
         {
             target.GetComponent<Renderer>().material = immunityMaterial;
         }
         else
         {
             target.GetComponent<Renderer>().material = pathogenMaterial;
         }

         //if dead, die & respawn
         if (this.health < 0)
         {
             Debug.Log(target.name + " died.");
             Destroy(thisPlayer);
             Respawn(5);
         }

          //***THIS IS THE COROUTINE***
          IEnumerator Wait(float duration)
          {
          yield return new WaitForSeconds(duration);   //Wait
          }


P.S. - The whole public class is derived from NetworkBehaviour. Maybe that's the problem?

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

1 Reply

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

Answer by Addyarb · Nov 02, 2015 at 06:37 PM

Coroutines were the single most difficult thing for me to understand getting started, so I hope I can relieve some of the frustration.

StartCoroutine() does not make your code pause, it simply starts a method that has the ability to pause - but ONLY in that method.

The way you've got it commented out makes sense, but it doesn't work like that. You have to put the code you wish to separate inside the coroutine. Like so:

 float health;
     Transform Player,target;
     public Material immunityMaterial,pathogenMaterial;
 
     void Respawn(){
         //Do respawn
     }
     void DealDamage(GameObject target, int amount){
         StartCoroutine (DamageEffect(target,amount));
     }
 
     IEnumerator DamageEffect(GameObject target, int amount){
         this.health-=amount;
         target.GetComponent<Renderer>().material.color = Color.red;
         yield return new WaitForSeconds(0.5f);
         if (target.tag == "Immunity")
             target.GetComponent<Renderer>().material = immunityMaterial;
         else
             target.GetComponent<Renderer>().material = pathogenMaterial;
         if (this.health < 0)        //if dead, die & respawn
         {
             Debug.Log(target.name + " died.");
             Respawn(5);
             Destroy(this.Player);
         }

P.S. I moved the Destroy(this.Player) to after the Respawn(5) because once you destroy your object, you also destroy this instance of code - meaning Destroy() better be the last thing you call, if it's destroying the object that this script is on. There's a way around that - by using a delay. Destroy(this.Player,5) waits 5 seconds and then destroys the Player, for instance.

But I digress...

If you're like me, you're wondering at this point "That seems really redundant just to get a 0.5f second gap inbetween my code. Well that's C# for ya!

Only call a coroutine on the actual object the IEnumerator is on. The coroutine itself resides on the caller, not the recipient. As long as they're both the same thing, everything is fine. However, if you were to call a coroutine on something, and then you got destroyed, you would halt the coroutine on that thing indefinitely.

For clarification, see the thread I learned about it the hard way!

Good luck with your game!

http://answers.unity3d.com/questions/1071957/ienumerator-stalling-if-supplied-with-int.html#comment-1072795

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 shrey150 · Nov 02, 2015 at 10:20 PM 0
Share

Yeah, the Destroy() and Respawn() thing I just threw in to show what I was gonna do there, didn't actually code that yet :) Thanks for the help, just modified your code so that Cmd_Shoot() runs the DealDamage() coroutine straight from inside it. Thanks again!

avatar image shrey150 · Nov 07, 2015 at 01:05 AM 0
Share

Hey, sorry about not accepting your answer as the best one, kinda forgot :(

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Coroutine working in Play Mode, but not on build 0 Answers

c# Coroutines and Waypoints HELP PLS!!!,C# Coroutine and Waypoints Help pls!!! 2 Answers

Why isn't my coroutine working when I call it from another script. 0 Answers

Wait time after coroutine's wait seconds is complete 0 Answers

Coroutine WaitForSeconds ignoring StopAllCoroutines... How can I do it? 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