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

why my StartCoroutine failed!

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

  1. guoliulong

    guoliulong

    Joined:
    Aug 16, 2012
    Posts:
    14
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4. public class GetScreenPicture : MonoBehaviour {
    5.     void Start()
    6.     {
    7.         StartCoroutine("MyStartCor",2.0f);
    8.     }
    9.    
    10.     IEnumerator MyStartCor()
    11.     {
    12.         yield  return WaitForMySecond(1);
    13.     }
    14.     IEnumerator WaitForMySecond(float seconds)
    15.     {
    16.         yield return new  WaitForSeconds(seconds);
    17.         print("########################################################");
    18.     }
    19. }
     
  2. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
    You need to add a new before WaitForSeconds.
    Code (csharp):
    1.  yield  return new WaitForMySecond(1);
    Also, your function does not take in variables so why are you assigning 2.0f?
     
  3. guoliulong

    guoliulong

    Joined:
    Aug 16, 2012
    Posts:
    14
    "assigning 2.0f" that's unnecessary! that' my failed!
    but if adding new after return i will get complier error :

    Assets/Script/GetScreenPicture.cs(21,35): error CS0118: `GetScreenPicture.WaitForMySecond(float)' is a `method' but a `type' was expected
     
  4. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
    Replace 1 with 1.0f. 1 is an integer, putting it as 1.0f means 1.0 is a float.
     
  5. guoliulong

    guoliulong

    Joined:
    Aug 16, 2012
    Posts:
    14
    now i replace 1 with 1.0f.

    if i leave "new " there . there still the same error
    if i delete "new" . there is no error but the code not work。。。。 you may test it
     
  6. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
    Can you post the updated codes AND the error that you get?
    *EDITED*
    I think I know where you went wrong. First of all, when you assign the value to a IENumerator function, you put it in the brackets, not externally.
    Code (csharp):
    1. StartCoroutine(WaitForMySecond(2.0f));
    I had thought you were using the first function which was MyStartCor. Sorry, error on my part. I didn't read properly.
     
  7. guoliulong

    guoliulong

    Joined:
    Aug 16, 2012
    Posts:
    14
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.IO;
    5. public class GetScreenPicture : MonoBehaviour {
    6.  
    7.     //private int ii = 0;
    8.     // Use this for initialization
    9.     void Start()
    10.     {
    11.         //yield  return WaitForMySecond(1);
    12.         StartCoroutine("MyStartCor");
    13.         //yield return null;
    14.         //ii++;
    15.         //StartCoroutine("GetPicture");
    16.         //StartCoroutine("ExcuteNextFrame");
    17.         //myprint("1");
    18.     }
    19.    
    20.     IEnumerator MyStartCor()
    21.     {
    22.         yield  return new  WaitForMySecond(1.0f);
    23.     }
    24.    
    25.    
    26.     // Update is called once per frame
    27.     //void Update () {
    28.         //ii++;
    29.     //  print("ii:"+ii);
    30.     //}
    31.    
    32.    
    33.     //IEnumerator GetPicture()
    34.     //{
    35.     //  yield return new WaitForEndOfFrame();
    36.     //  Texture2D pic = new  Texture2D(Screen.width,Screen.height);
    37.        
    38.     //  pic.ReadPixels(new Rect(0,0,Screen.width,Screen.height),0,0);
    39.     //  byte []picdata = pic.EncodeToPNG();
    40.         //pic.Apply();
    41.        
    42.     //  File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png",picdata);
    43.        
    44.     //}
    45.    
    46.     //IEnumerator ExcuteNextFrame()
    47.     //{
    48.     //  myprint("1");
    49.     //  yield return null;
    50.     //  myprint("2");
    51.     //  yield return null;
    52.     //  myprint("3");
    53.     //  yield return null;
    54.     //  myprint("4");
    55.     //}
    56.    
    57.     //void myprint(string str)
    58.     //{
    59.     //  print(str);
    60.     //}
    61.    
    62.     IEnumerator WaitForMySecond(float seconds)
    63.     {
    64.         yield return new  WaitForSeconds(seconds);
    65.         print("########################################################");
    66.     }
    67. }
    68.  
    69.  
    70.  
     
  8. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
    Okay, first of all, why are you yield returning WaitForMySeconds in another IENumerator function?
    From what I'm seeing, get rid of MyStarCor and use WaitForMySecond only. Assign a float and StartCoroutine for that function only.

    Code (csharp):
    1.  
    2. void Start()
    3.     {
    4.         StartCoroutine(WaitForMySecond(1.0f));
    5.     }
    6.  
    7.  
    8. IEnumerator WaitForMySecond(float seconds)
    9.     {
    10.         yield return new  WaitForSeconds(seconds);
    11.  
    12.         print("########################################################");
    13.     }
    14.  
     
    Last edited: Aug 17, 2012
  9. guoliulong

    guoliulong

    Joined:
    Aug 16, 2012
    Posts:
    14
    haha,i want write it like this。

    but it not work。

    Could you help me .......?
     
  10. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
    You can use the string version as it turns out (I should read more about StartCoroutine).
    Code (csharp):
    1.  
    2. void Start()
    3.     {
    4.         StartCoroutine("WaitForMySecond", 1.0f);
    5.     }
    6.  
    7. IEnumerator WaitForMySecond(float seconds)
    8.     {
    9.         yield return new  WaitForSeconds(seconds);
    10.         print("########################################################");
    11.     }
    12.  
     
  11. guoliulong

    guoliulong

    Joined:
    Aug 16, 2012
    Posts:
    14
  12. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
    You're welcome.