Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 fax58 · Apr 23 at 11:17 AM · coroutine

coroutine working with keyboard but not with bool

Hi All!

I'm fairly new to Unity and I'm trying to implement a simple 3d shooter.

Yesterday I spent literally all day with a couroutine, trying to make the player respawn after death. Today I started from 0 and made a super simple code and it's working, but when I integrated it with my game code still has problems.
Basically if I use the keyboard to respawn the player, everything works fine, but if I use a boolean it doesn't. I can work around this and make it work in another way, but at this point I would like to know why it has this behaviour since I'm still learning.

So, from my GameManager script I'm calling this coroutine:

   void Update() {
         if (Input.GetKeyDown(KeyCode.K)) {
             playerController.EliminatePlayer();
         }
         if (!playerController.isAlive) { // Input.GetKeyDown(KeyCode.J) 
 
             playerController.gameObject.SetActive(superBool);              
             StartCoroutine(Hide());
         }
     }
     private IEnumerator Hide() {
         playerController.ActivateShield();
         for (int i = 0; i < 6; i++) {
             superBool = !superBool;
             yield return new WaitForSeconds(0.5f);            
             playerController.gameObject.SetActive(superBool);            
         }
         playerController.gameObject.SetActive(true);
         playerController.isAlive = true;
         playerController.DeactivateShieldInstant();
         superBool = true;
     }


and the method in the playerController, EliminatePlayer, is just this one:

 public void EliminatePlayer() {
         gameObject.SetActive(false);
         isAlive = false;
         Instantiate(baseExplosion, transform.position, baseExplosion.transform.rotation);        
     }


Now: if in the Update() I put the commented code (in the second if) works as expected, if I leave the code as it is, the gameObject is blinking very fast, not once every half second as it should. I've been using the isAlive boolean in other script and I never had a problem, so I'm guessing it has something to do with the coroutine.

Any Ideas?

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
1
Best Answer

Answer by towkop · Apr 23 at 11:34 AM

 private bool hideRunning = false;
   void Update() {
          if (Input.GetKeyDown(KeyCode.K)) {
              playerController.EliminatePlayer();
          }
          if (!playerController.isAlive&&!hideRunning) { // Input.GetKeyDown(KeyCode.J) 
  
              playerController.gameObject.SetActive(superBool);
              StartCoroutine(Hide());
          }
      }
      private IEnumerator Hide() {
          hideRunning = true;
          playerController.ActivateShield();
          for (int i = 0; i < 6; i++) {
              superBool = !superBool;
              yield return new WaitForSeconds(0.5f);            
              playerController.gameObject.SetActive(superBool);            
          }
          playerController.gameObject.SetActive(true);
          playerController.isAlive = true;
          playerController.DeactivateShieldInstant();
          superBool = true;
          hideRunning = false;
      }
 

What the problem might be is that every frame the player is dead you are starting a new Hide() coroutine. If you add a variable storing info if coroutine is running then everything should work as expected

Comment
Add comment · Show 1 · 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 fax58 · Apr 23 at 01:06 PM 0
Share

Thank you towkop, is working perfectly!

And now that I know the solution it actually make perfect sense!

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

146 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 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

Coroutine does not work. 1 Answer

Problem with coroutine. 2 Answers

How to instantly restart a coroutine? 1 Answer

Staggered Animations - Do I use Pre-fabs ? 1 Answer

SpeedBoost won't reset : Problem with either WaitForSeconds or Coroutine (Solved) 2 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