Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

C#and js not work out the same result

Discussion in 'Scripting' started by guoliulong, Aug 17, 2012.

  1. guoliulong

    guoliulong

    Joined:
    Aug 16, 2012
    Posts:
    14
    here the code from
    http://docs.unity3d.com/Documentation/ScriptReference/Coroutine.html

    js run result:
    Starting 0
    WaitAndPrint 5.002409
    Done 5.002409

    but C#‘s run result :
    Starting 0
    Done 0.02

    Is there any wrong?

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class example : MonoBehaviour {
    5.     IEnumerator WaitAndPrint() {
    6.         yield return new WaitForSeconds(5);
    7.         print("WaitAndPrint " + Time.time);
    8.     }
    9.     IEnumerator Example() {
    10.         print("Starting " + Time.time);
    11.         yield return WaitAndPrint();
    12.         print("Done " + Time.time);
    13.     }
    14.  
    15.         //my code
    16.     void Start()
    17.     {
    18.         StartCoroutine(Example());
    19.     }
    20.  
    21. }
    Code (csharp):
    1.  
    2. // - prints "Starting 0.0"
    3. // - prints "WaitAndPrint 5.0"
    4. // - prints "Done 5.0"
    5.  
    6. print ("Starting " + Time.time);
    7. // Start function WaitAndPrint as a coroutine
    8. yield WaitAndPrint();
    9. print ("Done " + Time.time);
    10.  
    11. function WaitAndPrint () {
    12.     // suspend execution for 5 seconds
    13.     yield WaitForSeconds (5);
    14.     print ("WaitAndPrint "+ Time.time);
    15. }
    16.  
     
  2. UNITY3D_TEAM

    UNITY3D_TEAM

    Joined:
    Apr 23, 2012
    Posts:
    720
    u given 5 in c# .u want to assign f=for floating point values in c#.that's why u getting that difference
     
  3. guoliulong

    guoliulong

    Joined:
    Aug 16, 2012
    Posts:
    14
    my code still not work right
    you can test it
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class example : MonoBehaviour {
    6.     IEnumerator WaitAndPrint() {
    7.         yield return new WaitForSeconds(5.0F);
    8.         print("WaitAndPrint " + Time.time);
    9.     }
    10.     IEnumerator Example(){
    11.         print("Starting " + Time.time);
    12.         yield return WaitAndPrint();
    13.         print("Done " + Time.time);
    14.     }
    15.    
    16.     void Start()
    17.     {
    18.         StartCoroutine(Example());
    19.     }
    20. }
    21.  
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,400
    Not at all, it's just implicitly converted to a float anyway.

    C# needs to use StartCoroutine. Also that's more complex than it needs to be since Start can be a coroutine.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Example : MonoBehaviour {
    5.  
    6.     IEnumerator WaitAndPrint() {
    7.         yield return new WaitForSeconds(5);
    8.         print("WaitAndPrint " + Time.time);
    9.     }
    10.  
    11.     IEnumerator Start() {
    12.         print("Starting " + Time.time);
    13.         yield return StartCoroutine(WaitAndPrint());
    14.         print("Done " + Time.time);
    15.     }
    16. }
    --Eric