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 /
avatar image
0
Question by TheSOULDev · Aug 14, 2017 at 10:21 PM · coroutinewait

I want to create a coroutine that will run a series of functions when a set variable reaches its value. What would be the best way to do this?

So, like the title said, I want to create some kind of IEnumerator that will wait until my first parameter's value changes to my second argument, the goal value, and run the third argument which will be a delegate (called ToRun, has no arguments).

I had created something like this:

 public IEnumerator WaitForValue (float Parameter, float Value, ToRun Queue)
     {
         if (Queue != null)
         {
             yield return new WaitUntil(() => Mathf.Abs(Parameter - Value) <= 0.02f);
             Queue();
         }
 
         yield return null;
     }

but it only works for certain objects (for some reason it works for color components, but doesn't for regular floats). Now, maybe there is some other error that I do not see, but seeing as how values are passed in C#, floats are passed by value, not reference, therefore my coroutine shouldn't work for floats since I'm passing just the value of the original float I'm looking at, but it doesn't get updated. Now, I don't know why my coroutine works for sure, but I also do not know why it doesn't work in some cases (I checked with debug log, event is not triggered even though I see the parameter reach the desired value). In fact, it works when my Value argument is 0f, but somehow doesn't when it is 1f (even though the 0.02f threshold is large enough to accept float mistakes which were -0.00093 in the most extreme cases).

So, I guess I'm doing this sub-optimally. I use this coroutine to order my transitions around, like some sort of a state machine controller. I reckon I will need this method and its overloads later in the game I'm making as well because I don't want to constantly run checks in Update.

So, does the best way (or at least a better way) to do this in C# exist since I can't force ref values for WaitUntil?

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

0 Replies

· Add your reply
  • Sort: 

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

70 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

Related Questions

Alternative for semaphores in Unity? 2 Answers

Move a gameobject smoothly without update, without coroutine 2 Answers

Help with waiting for input? I'm too used to how Console.ReadLine() works. 0 Answers

A simple Wait-Function without Coroutine (C#) 12 Answers

Wierd issue with Coroutines? 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