Search Unity

Question Coroutine as variables -- storing parameter

Discussion in 'Scripting' started by KarlKarl2000, Jun 22, 2020.

  1. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    Hi guys,

    I need some help. I don't understand how to pass a parameter to a coroutine. I'm going about this way because I'd like to use StopCoroutine(_coRoute )

    Thanks for the help!

    Code (CSharp):
    1. private   IEnumerator _coRoute;
    2.  
    3. private void OnEnable()
    4. {
    5.         _coRoute =  MethodMethod();
    6.         //HERE UNITY GIVES ME RED ERROR.. and I can't use _aBool
    7. }
    8.  
    9. private void MethodMethod(bool _aBool)
    10. {
    11.         //stuff
    12. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
  3. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    Yea I was looking at that for days and couldn't wrap my heard around it ... but now that I think about it... is this the only way to do it?

    Visual Studio doesn't throw any errors anymore. I guess this is it :rolleyes:
    Thanks!
    Code (CSharp):
    1. [code=CSharp]
    2.  
    3. private   IEnumerator _coRouteTrue;
    4. private   IEnumerator _coRouteFalse;
    5.  
    6. private void OnEnable()
    7. {
    8.         _coRouteTrue =  MethodMethod(true);
    9.         _coRouteFalse=  MethodMethod(false);
    10. }
    11.  
    12. private void MethodMethod(bool _aBool)
    13. {
    14.         //stuff
    15. }
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    That does match their example. Technically though, you can assign the value of the variable whenever, so you don't actually have to have 2 IEnumerator variables unless you're running both at the same time.
     
  5. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    That's the thing, I wasn't able to figure out how to do that during Start(). I guess I'll just have 2 variables .. Thanks
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Your coroutine's return type needs to be IEnumerator.
     
  7. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    Sorry I don't understand. . . :eek:

    Thanks anyways!
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Oh yep, @PraetorBlue was correct and I overlooked when I said it matches their example. You should double check the link I shared to see what is different. Otherwise, it has to do with your method return type.
     
  9. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    Ohhh right.. typo on my end when I was inputting the code

    yea my bad.


    And I finally understood what you meant by this .. you're right.... making it a public variable would do the trick :oops: