Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 user5200 · Jun 25, 2012 at 11:19 PM · c#coroutinebooleancoroutinestoggle

Toggling bools automatically using coroutines

Hello all,

Just a quick question from a programming noob.

I have encountered many situations where a bool needs to turn itself on or off after a given amount of time and so I thought I could make a coroutine that can act as a switch for any bool. Here is what I tried:

 IEnumerator ToggleBool (bool thebool, float time)
     {
         thebool = true;
         
         yield return new WaitForSeconds(time);
         
         thebool = false;
         
     }

Here is how this would be used in Update:

 if (firsttextdelay - Time.deltaTime >= 5)
             StartCoroutine ( ToggleBool (welcomeon, 5) );

After a delay of 5 seconds, this would turn on welcomeon and the coroutine should make it turn itself off 5 seconds later. However, this does not work, while specifically putting welcomeon in the coroutine works. Like this for example:

 IEnumerator ToggleBoolz (float time)
     {
         welcomeon = true;
         
         yield return new WaitForSeconds(time);
         
         welcomeon = false;
         
     } 

Is it just that coroutines do not work with bools? Or am I doing something wrong?

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

Answer by Mortoc · Jun 25, 2012 at 11:26 PM

bool and floats are passed by value whereas objects are passed by reference. So the bool and float you modify in your coroutines aren't the same variables that you passed in anymore, they're copies.

http://msdn.microsoft.com/en-us/library/0f66670z(v=vs.71).aspx

You can almost make this work:

 IEnumerator ToggleBool(ref bool test, float time)
 {
     test = false;
     yield return new WaitForSeconds(time);
     test = true;
 }
     

But C# doesn't support ref or out variables from coroutines, so it looks like you can't make a general use function for this.

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 user5200 · Jun 26, 2012 at 12:49 AM 0
Share

What a bummer :( Just when I thought I did something smart. Oh well, thanks for the insight.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a way to find out when a coroutine is done executing? 3 Answers

Trouble Resuming after Yielding while Inside Coroutine 1 Answer

Generic Cooldown IEnumerator? 1 Answer

Running a coroutine without StartCoroutine 1 Answer

Using Time efficiently regardless of current frame rate. 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