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 bakill15 · Apr 15, 2016 at 01:21 PM · yield waitforsecondsbreak

StartCoroutine still runs 1 more second after using yield break

I tried using the StartCoroutine to make my pet smile for 1 second when clicked, but after 1 second more, the clicked bool returns several false value for 1 second more. I put yield break after the clicked is returned a false value to break it immediately but still it runs for 1 second more. How do i break it instantly?alt text

 IEnumerator PetTouch()
     {
         if (clicked)
         {
             emoteMats[1] = emoteMattouched;
             this.GetComponent<Renderer>().materials = emoteMats;
             yield return new WaitForSeconds(1);
             clicked = false;
             Debug.Log(clicked);
             yield break;
         }
     }


logs.png (18.6 kB)
Comment
Add comment · Show 2
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 meat5000 ♦ · Apr 15, 2016 at 03:50 PM 0
Share

Hmm what happens if you yield from inside a statement then make that statement inaccessible?

avatar image meat5000 ♦ · Apr 15, 2016 at 05:30 PM 0
Share

Your answerers are correct.

You should aim to place a condition on the calling of the Coroutine rather than checking a condition whilst it is running.

1 Reply

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

Answer by KaushikRahul · Apr 15, 2016 at 01:52 PM

From what i can see, Whats happening here is:

If you observe closely the mouse click is registered on each and every single frame. For example if you hold the mouse click down for a second and you game is running on 60 FPS then total number of clicks detected will be 60. Keep this in mind when you are making a call to your Enumerator. For every Fame your Co-routine will be called which in turn will result and extra second of output (in your case).

so if you are using Input.GetMouseButton(0), please don't, instead use Input.GetMouseButtonDown(0). It will solve your problem for sure.

:) :)

Comment
Add comment · Show 5 · 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 bakill15 · Apr 15, 2016 at 02:06 PM 0
Share

Im actually using the On$$anonymous$$ouseDown function and it's not the problem since it returns only 1 value per click. I really think it's in the coroutine i just don't know how.

 void On$$anonymous$$ouseDown()
         {
             changingEmote = true;
             if(!clicked)
             {
                 Debug.Log("You clicked on your pet");
                 clicked = true;
             }
                    
             changingEmote = false;
         }

alt text

logs.png (9.1 kB)
avatar image KaushikRahul bakill15 · Apr 15, 2016 at 02:26 PM 0
Share

Its working absolutely fine for me.

See the code below:

     void On$$anonymous$$ouseDown()
     {
         StartCoroutine($$anonymous$$ouseClick());
     }
 
     IEnumerator $$anonymous$$ouseClick()
     {
         yield return new WaitForSeconds(1.0f);
         Debug.Log("I Am Here!!");
     }

Even your code is working fine for me :

     public bool clicked = true;
 
     void On$$anonymous$$ouseDown()
     {
         StartCoroutine($$anonymous$$ouseClick());
     }
 
     IEnumerator $$anonymous$$ouseClick()
     {
         if (clicked)
         {
             yield return new WaitForSeconds(1);
             clicked = false;
             Debug.Log(clicked);
             yield break;
         }
     }


There might be something else, its not the co-routine.

avatar image bakill15 KaushikRahul · Apr 15, 2016 at 02:41 PM 0
Share

Yeah the problem was i executed/run the coroutine in the update function. Running it inside On$$anonymous$$ouseDown function worked but give me some other problems but i think i could work with those. These pet just got several emotion that i have to make several bools for them to work properly in different conditions. Anyways, your answer helped in this specific problem.

Thanks mate :D

avatar image NoseKills bakill15 · Apr 15, 2016 at 02:38 PM 0
Share

Your IEnumerator method has only 1 Debug.Log call and no looping of any kind so the fact that you get many logs tells me you have many coroutines running.

You set clicked to true when you click and i guess you create coroutines in Update as long as clicked stays true. You should only create 1 coroutine in On$$anonymous$$ouseDown

You start many coroutines and when you set clicked to false in one of them, it doesn't do anything to the ones that are already past the if-check and in the state where they wait for 1 second. After they finish waiting, they print 'false'

The last yield break; in the method doesn't do anything because it's the last thing in the method. The execution will exit the method anyways even without the break

avatar image KaushikRahul NoseKills · Apr 15, 2016 at 02:40 PM 0
Share

@Nose$$anonymous$$ills I know... its just the same code is working for me but not for him. So the only reason could be $$anonymous$$ultiple calls to the co-routine.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Break a GetButton 0 Answers

Trying to spawn random falling rocks, can't figure out why it's not working. Help would be appreciated! 2 Answers

removing an int after random use 0 Answers

EInumerator dont stop velocity I need 3sec to breake.. pls help 0 Answers

yield ends method 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