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 MasterChameleonGames · Jan 28, 2019 at 12:15 AM · coroutinecoroutinesienumerator

StopCoroutine() Not Working

Other solutions previously posted do not make it work either.

Saving the coroutine in a variable does nothing.

It prints "stopped" on the console, so the statement does return true when the mouse is released.

 void Update() {
     StartAndStopFiring();
 }

 void StartAndStopFiring()
     {
         // If LMB pressed
         if(Input.GetMouseButtonDown(0))
         {
             //Start shooting
             StartCoroutine(FireWeapon());
         }
         // If LMB released
         else if(Input.GetMouseButtonUp(0))
         {
             //Stop shooting
             StopCoroutine(FireWeapon());
             print("stopped");
         }
     }
 
     IEnumerator FireWeapon()
     {
         while(true)
         {
             Quaternion quaternion;
             if (isFacingRight)
             {
                 // Bullet moves right
                 quaternion = Quaternion.identity;
             }
             else
             {
                 // Bullet moves left
                 quaternion = Quaternion.Euler(0, 180, 0);
             }
 
             // Instantiates the bullet at the spawn point with the quaternion set earlier and the parent as the organizer
             Instantiate(bullet, bulletSpawn.position, quaternion, bulletParent);
 
 
             // For example, if the firingRate is 20, it waits 0.05 seconds
             yield return new WaitForSeconds(1 / firingRate);
 
         }
     }


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

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

Answer by Hellium · Jan 28, 2019 at 09:11 AM

When calling StooCoroutine, you must have a reference to the started coroutine:

  private IEnumerator fireCoroutine;
  void StartAndStopFiring()
  {
      // If LMB pressed
      if(Input.GetMouseButtonDown(0))
      {
          //Start shooting
          fireCoroutine = FireWeapon();
          StartCoroutine(fireCoroutine);
      }
      // If LMB released
      else if(Input.GetMouseButtonUp(0))
      {
          //Stop shooting
          StopCoroutine(fireCoroutine);
      }
  }
Comment
Add comment · Show 3 · 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 MasterChameleonGames · Jan 28, 2019 at 08:18 PM 1
Share

Already tried that, didn't work

avatar image Hellium MasterChameleonGames · Jan 28, 2019 at 08:35 PM 0
Share

It does work. I've just tried it, and the coroutine stops running when the left mouse button is released.

avatar image MasterChameleonGames Hellium · Jan 28, 2019 at 08:52 PM 0
Share

Uh, I guess the same exact code that didn't work 12 hours ago and 23 $$anonymous$$utes ago works now. It's working flawlessly now for some reason. Thanks for the help.

avatar image
3

Answer by tormentoarmagedoom · Jan 28, 2019 at 08:44 AM

Good day.

First, Why you encapsule all FireWeapon in a while (true) ?? its the same as nothing...

Anyway. You are using GetMouseButtonDown wich only is executed the 1st frame the button is clicked. And, as i dont see the method calls itself again, it will only be executed once if you hold the button.

As the FireWeapon() is only executed once, when it reached the end, it does not need to be "stopped"

I recommend you to just call itself again right after the yield command

 [...]
          // For example, if the firingRate is 20, it waits 0.05 seconds
          yield return new WaitForSeconds(1 / firingRate);
           StartCoroutine(FireWeapon());

And remove that while (true) that makes nothing!

Good luck!

Bye!

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 MasterChameleonGames · Jan 28, 2019 at 08:21 PM 0
Share

Actually, while(true) is doing something, it's there so the coroutine would repeat until it is told to stop. FireWeapon() is not executed once, it is executed until an external command tells it to stop (which is not working).

Your confusion might be from me not putting the Update() function, my bad.

Calling a function inside itself is not the best idea.

But I'll update the code so that it's clear.

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

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

"Can't add script behaviour AICharacterControl. The script needs to derive from MonoBehaviour!" ? 0 Answers

How to get the Progress from a IEnumerator 0 Answers

IEnumerator inner functions 0 Answers

Add points every five seconds 1 Answer

Coroutine not working on android build 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