Search Unity

yield StartCoroutine not compiling, yield return works fine

Discussion in 'Scripting' started by omatase, Aug 30, 2014.

  1. omatase

    omatase

    Joined:
    Jul 31, 2014
    Posts:
    159
    I've found code online that shows you can call yield StartCoroutine but when I try to use it I get a compiler error saying it doesn't recognize the type or namespace yield. Searching for this returns nothing and I can't find anyone saying yield StartCoroutine should not work but I'm beginning to think that maybe it's just not supported in the latest versions of Unity anymore. Any help appreciated. Here's my code. This is my login scene and basically what I'm trying to do is start a process that waits on WWW then load the next scene once it returns. The WWW code is encapsulated in director.GetAccountDetails. The compliation error occurs on line 18.

    Although yield StartCoroutine doesn't work, other calls to yield are fine such as "yield return".

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using System.Collections;
    4. using Assets.Directors;
    5.  
    6. public class Login : MonoBehaviour {
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.  
    11.         GetAccountDetails();
    12.     }
    13.  
    14.     IEnumerator GetAccountDetails()
    15.     {
    16.         Account director = gameObject.AddComponent("Account") as Account;
    17.  
    18.         yield StartCoroutine(director.GetAccountDetails());
    19.  
    20.         // load the next scene
    21.         Application.LoadLevel("ManageTeams");
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update () {
    26.    
    27.     }
    28.  
    29.     void OnGui()
    30.     {
    31.         GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 15, 200, 30), "Loading...");
    32.     }
    33. }
    34.  
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That's not correct syntax for C#; you need to use "yield return new". See the docs.

    --Eric
     
  3. omatase

    omatase

    Joined:
    Jul 31, 2014
    Posts:
    159
    Oh, OK, thanks. I was thinking it was probably something simple like that. I was able to fix it by changing my code to.
    Code (CSharp):
    1. yield return StartCoroutine(director.GetAccountDetails());
    Thanks!