• 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
Question by OctoMan · Nov 03, 2015 at 07:37 PM · ienumeratorbreak

Leave/End the IEnumerator the right way.

Hi there,

i have an IEnumerator which has to do a ColorCheck.

 IEnumerator StartColorCheck()
 {
     if(checkingColors)
     {
         //Debug.Log ("can't StartColorCkeck");
         yield break;
     }
     checkingColors = true;

     yield return new WaitForSeconds (0.5f);

     for (int i = 0; i < allcards.Length; i++)
     {
         CC = allcards[i].GetComponent<ColorCheck>();
         CC.CheckColors();

         CardMatches += CC.matches;
     }
     checkingColors = false;
 }

The Problem is, it seems to stack up, until all calls are gone through it. I call it from a

  void OnMouseUp()

but when i click to fast for example, all clicks seem to stack until all ran through it. Is there a way to avoid that? Because CardMatches returns a wrong value while it's repeating all over and over.

Thanks in advance.

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by Statement · Nov 03, 2015 at 08:23 PM

I don't really understand your problem. Doesn't the above example work?

Note you could also replace that with a normal function and use Invoke.

 void OnMouse()
 {
      SingleInvoke("ColorCheck", 0.5f); // Call ColorCheck after half a second.
 }
 
 void SingleInvoke(string method, float time)
 {
     CancelInvoke(method); // If method is pending, cancel it.
     Invoke(method, time);
 }
 
 void ColorCheck()
 {
      for (int i = 0; i < allcards.Length; i++)
      {
          CC = allcards[i].GetComponent<ColorCheck>();
          CC.CheckColors();
          CardMatches += CC.matches;
      }
 }

CancelInvoke, Invoke

Comment

People who like this

0 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 OctoMan · Nov 03, 2015 at 08:32 PM 0
Share

Basicly all works yes, but if i click to fast it mess up the result of CardMatches since CheckColors seem to count to many.

Maybe it's be cause my OnMouse()

 void OnMouseUp()
     {
         if (isdragged && hitPlayfield) 
         {
             transform.position = newPosition;
             isdragged = false;
             //play sound
             CardAudio.clip = SwapClip;
             CardAudio.Play();
             //particles
             Instantiate(ParticleFX,newPosition + Vector3.up * 0.2f,Quaternion.identity);
             //do colorcheck in gamemanager
             GMS.InitColorCheck();
         }
         else if (isdragged && hitCard) 
         {
             transform.position = newPosition;
             DT.transform.position = oldPosition;
             isdragged = false;
             //play sound
             CardAudio.clip = SwapClip;
             CardAudio.Play();
             //particles
             Instantiate(ParticleFX,newPosition + Vector3.up * 0.2f,Quaternion.identity);
             //do colorcheck in gamemanager
             GMS.InitColorCheck();
 
         }
         else//just rotated
         {
             transform.position = oldPosition;
             isdragged = false;
             //play sound
             CardAudio.clip = RotateClip;
             CardAudio.Play();
             //Instantiate(ParticleFX,oldPosition + Vector3.up * 0.2f,Quaternion.identity);
 
             //do colorcheck in gamemanager
             //GMS.InitColorCheck();
             //do it in rotatescript
             if(!RS.isRotating)
                 GMS.InitColorCheck ();
                 Debug.Log ("rotation corouine fired");
         }
     }


I'll try your way with invoke.

avatar image Statement OctoMan · Nov 03, 2015 at 08:39 PM 0
Share

It shouldn't make a difference. But are you setting CardMatches to zero? It seems that you just keep adding and adding matches. I don't know if that is correct behaviour? If you had 3 matches, drag something, and you still have 3 matches, your variable will now be 3+3.

avatar image OctoMan Statement · Nov 03, 2015 at 08:41 PM 0
Share

Yes is reset the CardMatches onMouseDown()

and call this function from there:

 public void ResetCardMatches()
     {
         //if (!gameWon) 
         {
             CardMatches = 0;
             for (int i = 0; i < allcards.Length; i++) 
             {
                 allcards [i].GetComponent<ColorCheck> ().matches = 0;
                 //deactivate all highlights
                 allcards [i].GetComponent<ColorCheck> ().Highlight1.SetActive (false);
                 allcards [i].GetComponent<ColorCheck> ().Highlight2.SetActive (false);
             }
 
         }
     }

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

31 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

Related Questions

IENumerator does not work 0 Answers

IEnumerator keeps saying not all code paths return values 1 Answer

Pause a Game Object using Raycast and Sphere Collider? 0 Answers

I need help with an IEnumerator. 2 Answers

My code is not working - HELP - IEnumerator Coroutines 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