Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

coroutine inside a class

Discussion in 'Scripting' started by Brian-Kehrer, Dec 3, 2007.

  1. Brian-Kehrer

    Brian-Kehrer

    Joined:
    Nov 7, 2006
    Posts:
    411
    Is it possible to call a coroutine inside a class?

    This fails, but never triggers an error message, nor a compile error. Removing the yields, it works.

    I guess this is the same as a yield in a static function, which cannot work. I don't understand why, but I can accept it. At the very least it would be good for the editor to trigger an error message.


    Code (csharp):
    1.  
    2. //attached to a GUITexture
    3. function OnMouseDown () {
    4.    
    5.     var xyz = new DoThis(Time.time);
    6.     yield xyz.ReturnThis();
    7.  
    8. }
    9.  
    10. //the class
    11. class DoThis{
    12.     private var xy : int;
    13.    
    14.     function DoThis(xy){
    15.         this.xy = xy * 4;
    16.     }
    17.    
    18.     function ReturnThis() {
    19.         yield WaitForSeconds (.1);
    20.         Debug.Log(xy);
    21.        
    22.     }
    23.    
    24. }
    25.  
     
  2. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    Classes must derive from MonoBehaviour in order to inherit the functionality required to support Unity's coroutines, so this custom class with no base class doesn't qualify.
     
  3. Brian-Kehrer

    Brian-Kehrer

    Joined:
    Nov 7, 2006
    Posts:
    411
    is this what you mean?
    Is there a syntax to specify a base class in js? This below does not compile.


    class test : MonoBehaviour {

    }
     
  4. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    I think it would be something like this:

    Code (csharp):
    1. class Test extends MonoBehvaiour
    2. {
    3. }
     
  5. Brian-Kehrer

    Brian-Kehrer

    Joined:
    Nov 7, 2006
    Posts:
    411
    aha.. that does it....
    An interesting tidbit

    Code (csharp):
    1.  
    2.  
    3. StartCoroutine (temp.ReturnThis());
    4. //temp.ReturnThis();
    5.  
    6.  
    Calling the function directly leads to a null reference exception, whereas StartCoroutine doesn't
     
  6. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    That's interesting... so you can do that even on a class which doesn't derive from MonoBehaviour?
     
  7. Brian-Kehrer

    Brian-Kehrer

    Joined:
    Nov 7, 2006
    Posts:
    411
    No, you need the monoBehaviour--thanks for the help on that, I couldn't for my life find 'extends' in the docs.


    the bit about Start Coroutine strikes me as odd, since Joachim always told us in JS this was unnecessary. although it's very likely I missed the "except when..." part.

    It's worth noting, you can't seem to call another coroutine from a coroutine in a class, at least using the method described. I'm not a CS guy, so I'm bumbling through this, sorry.

    Anyway, I can now use
    Code (csharp):
    1.  
    2. yield StartCoroutine (class coroutine());
    3.  
    So I'm happy :)