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
1
Question by HTime12 · Apr 03, 2018 at 11:03 PM · coroutinereferenceboolmethod

reset bool on timer passed by ref

wanted to use a Coroutine to reset a bool value after a fixed time but it seams you cant pass in a ref to a IEnumerator

example

 class foo
 {
 bool 1;
 bool 2;
 bool 3;
 
 
 private bar()
 {
 // check something depending on check use bool 1 , 2 ,3 eg
 
 
 1 = true;
 StartCoroutine(Resetbool(1))
 }
 
 IEnumerator Resetbool(ref bool b)
 {
 yelid return new WaitForSeconds(5f)
 b = false;
 }
    
 }

it doesn't have to be a coroutine im just trying to avoid haveing more then one method to reset each bool

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 Cornelis-de-Jager · Apr 04, 2018 at 12:52 AM 0
Share

$$anonymous$$aybe try a switch with an int:

IEnumerator Resetbool(int i) { yelid return new WaitForSeconds(5f) switch (i) { case (1): a = false;break; case (2): b = false;break; ... } }

}

avatar image losingisfun · Apr 04, 2018 at 12:55 AM 0
Share

you need to pass it as a ref as well don't you? - e.g. StartCoroutine(Resetbool(ref 1))

1 Reply

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

Answer by Bunny83 · Apr 04, 2018 at 01:17 AM

Coroutines can not have ref parameters as all parameters of a coroutine get converted to member variables of an implicit closure class. Ref or out parameters can not be "stored" in variables. Ref and out parameters are their own kind of "type". They are actually pointers to the memory location of the variable. Because of that ref and out parameters can not "leave" their scope. If it would be possible to store them in a member variable they could outlive the extent of the variable which is not possible / allowed in managed code as this would give you an invalid pointer.


The only way to accomplish what you want to do is using an actual closure around the variable you want to change. So you could change the signature of your method to take a delegate which you would execute when the timer has expired. You can use a lambda expression to create the closure. Something like this:

 IEnumerator ExecuteIn5(System.Action aCallback)
 {
     yield return new WaitForSeconds(5f);
     aCallback();
 }
 // [ ... ]
 StartCoroutine(ExecuteIn5(()=>yourBool = false));
 StartCoroutine(ExecuteIn5(()=>yourOtherBool = false));

Another similar but more restrictive way would be:

 IEnumerator ResetBool(System.Action<bool> aCallback)
 {
     yield return new WaitForSeconds(5f);
     aCallback(false);
 }
 // [ ... ]
 StartCoroutine(ResetBool(a=>yourBool = a));
 StartCoroutine(ResetBool(a=>yourOtherBool = a));


Finally your code in question is very confusing as it's not valid C# code. You can not have variable names which start with a number (or just consist of a number)

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 HTime12 · Apr 04, 2018 at 12:15 PM 0
Share

Thank You! exactly what i was looking for

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

85 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

Related Questions

check if method is finished, bools not working 1 Answer

Call Functions Across Scripts, Null Object Error 1 Answer

Is reffrencing a huge class better than reffrencing it's elements? 1 Answer

Coroutine isn't working when game starts 1 Answer

Coroutine sequence not running properly 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