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

Preload an image

Discussion in 'Scripting' started by herbie, Jan 26, 2014.

  1. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    In my game an image is loaded using WWW to be used as a texture in a level.
    When the level is finished, the same level is reloaded again but another image is loaded using WWW.
    What I would like to have is that the new image will be loaded while the level is not finished yet.
    So when the level is finished, the new image can be used as a texture directly and you don't have to wait for downloading.

    I want to do that in this way:

    Code (csharp):
    1. function Start()
    2. {  
    3.     //Some code
    4.     image = new WWW ("image" + index + "1.png"); yield image; //index is increasing by 1 everytime
    5.     if (firstTime == 0)
    6.     {
    7.         firstTime = 1;
    8.         Next();
    9.     }
    10. }
    11.  
    12. function Next()
    13. {
    14.     Image.renderer.material.mainTexture = image.texture;
    15.     //Some other code
    16.     Start();
    17. }
    In another script called "gameFinished", the function Next is called and the new image can be used directly as a texture. At least, that's the idea.
    But of course, I get the error BCE0070: Definition of 'Start()' depends on 'Next()' whose type could not be resolved because of a cycle.

    How can I do this correctly?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Declare the return type for the function, which is IEnumerator since it's a coroutine. i.e., "function Start () : IEnumerator". Unity will normally infer the return type automatically, but that's not possible if you have functions that call each other recursively.

    --Eric
     
  3. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    It's working great now.
    Thanks!