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

Method with yield not executing

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

  1. omatase

    omatase

    Joined:
    Jul 31, 2014
    Posts:
    159
    I have the following code:

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using System.Collections;
    4. using Assets.Directors;
    5. using Assets;
    6.  
    7. public class Login : MonoBehaviour {
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.  
    12.         GetAccountDetails();
    13.     }
    14.    
    15.     IEnumerator GetAccountDetails()
    16.     {
    17.         WWW www = new WWW("http://localhost:12527/api/account");
    18.  
    19.         yield return www;
    20.  
    21.         byte[] data = Convert.FromBase64String(www.text.Replace("\"", string.Empty));
    22.  
    23.         Globals.AccountDetails = DirectorHelper.DeserializeFromNetwork<Battleschool.Common.Entities.Contract.AccountDetails>(data);
    24.  
    25.         //// load the next scene
    26.         Application.LoadLevel("ManageTeams");
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update () {
    31.        
    32.     }
    33.  
    34.     void OnGUI()
    35.     {
    36.         GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 15, 200, 30), "Loading...");
    37.     }
    38. }
    39.  
    When I run the scene I see the word "Loading..." but the next scene never loads. When I step into it execution goes from line 12 to line 15 and then just abruptly halts. The debugger doesn't crash or close, it just doesn't want to step into the function. I don't know what it doesn't like about the GetAccountDetails method but it never even executes the first line in it.
     
  2. omatase

    omatase

    Joined:
    Jul 31, 2014
    Posts:
    159
    Hmmm, didn't realize that I had to run the local method "GetAccountDetails" with StartCoroutine. Problem solved.