Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Nothing being called after coroutine.

Discussion in 'Scripting' started by TheLurkingDev, Sep 2, 2014.

  1. TheLurkingDev

    TheLurkingDev

    Joined:
    Nov 16, 2009
    Posts:
    91
    The following coroutine runs and successfully adds image files to a List<Texture2D>. However, no methods are called after running the coroutine.

    Code (CSharp):
    1. void Start()
    2.     {
    3.         CreateMap(10, 10);
    4.         SetTexturePaths();
    5.         GetFileNamesFromPaths();
    6.         StartCoroutine(GetTextures());
    7.  
    8.         DisplayGameTileTextures();
    9.     }
    Code (CSharp):
    1. IEnumerator GetTextures()  // Coroutine.
    2.     {
    3.         foreach(string fileName in _gameTileTexturesFileNames)
    4.         {
    5.             string filePath = _gameTileTexturesPath + fileName;
    6.             Debug.Log("filePath: " + filePath);
    7.             Byte[] byteFile = File.ReadAllBytes(filePath);
    8.             Tex = new Texture2D(256, 256);
    9.             yield return Tex.LoadImage(byteFile);
    10.             Tex.Apply();
    11.             _gameTileTextures.Add(Tex);
    12.         }
    13.         yield break;
    14.     }
    Do I need to somehow stop the coroutine? Or is it a different issue?
     
  2. TheLurkingDev

    TheLurkingDev

    Joined:
    Nov 16, 2009
    Posts:
    91
    Eh... I had to convert the next method called to a coroutine also in order for it to be called only after the first one stopped.
     
  3. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    If you want DisplayGameTileTextures to be called after the GetTextures returns, you need to make the Start method a coroutine and yield until the GetTextures function completes.
    Code (csharp):
    1. IEnumerator Start ()
    2. {
    3.     yield return StartCoroutine (GetTextures ());
    4.     DisplayGameTileTextures;
    5. }
     
    TheLurkingDev likes this.