Search Unity

Quick Coroutine yield C# question

Discussion in 'Scripting' started by TWest, Aug 19, 2009.

  1. TWest

    TWest

    Joined:
    May 26, 2009
    Posts:
    131
    I am converting the DragRigidbody.js script to C# for learning purposes more than anything else really, and I have ran into a snag. What Does the DragObject yield return?

    Code (csharp):
    1.  
    2. float oldDrag = springJoint.connectedBody.drag;
    3.     float oldAngularDrag = springJoint.connectedBody.angularDrag;
    4.     springJoint.connectedBody.drag=drag;
    5.     springJoint.connectedBody.angularDrag = angularDrag;
    6.     Camera mainCamera = Camera.main;  
    7.     while(Input.GetMouseButton(0))
    8.     {
    9.         Ray ray = new Ray(mainCamera.transform.position, mainCamera.transform.forward);
    10.         springJoint.transform.position = ray.GetPoint(distance);
    11.  
    12.         yield return; // not sure what to return and c# doesn't like this very much
    13.        
    14.     }
    15.     if(springJoint.connectedBody)
    16.     {
    17.         springJoint.connectedBody.drag = oldDrag;
    18.         springJoint.connectedBody.angularDrag = oldAngularDrag;
    19.         springJoint.connectedBody = null;
    20.     }
    21.  
    Any help would be much appreciated

    Thank You
    TWest
     
  2. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    I don't think it's documented anywhere, or at least I never was able to find documentation.

    However, in practice it appears that whenever you return something that is not inheriting from YieldInstruction (e.g. Coroutine, WWW or WaitForSeconds), it will just wait for the next frame.

    So if you want to translate a Unity Script yield; command, you can really return almost anything you like. Personally, I use yield return 1;.

    EDIT:
    the C# manual page does give an example (but does not explain anything):
    http://unity3d.com/support/documentation/ScriptReference/index.Writing_Scripts_in_Csharp.html
    In fact, the page even forgets to mention that C# coroutines don't work unless you explicitly use StartCoroutine() to start them.
     
  3. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,307
    Actually, in that case it's probably best to use yield return new WaitForEndOfFrame(); - otherwise, your C# code ends up being just as meaningless as the UnityScript code.

    That's the thing I find really beautiful about converting UnityScript-stuff to C#-code: After the conversion, you can read it and immediately know what it means ;-)
     
  4. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    Except that waiting for the end of a frame is not the same as waiting for the next one :p.
     
  5. TWest

    TWest

    Joined:
    May 26, 2009
    Posts:
    131
    The real issue is here what should I set the return value of the function as anything i put it says its not an iterator interface type, Im seeing what i can find now. Ill post if I can find it.


    Thank You

    As soon as I posted found it..IEnumerator
     
  6. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    The return type is IEnumerator. See the link I posted earlier.