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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Catch LoadLevel error

Discussion in 'Scripting' started by cecarlsen, Jan 15, 2008.

  1. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    848
    Hi Forum

    I simply want to check during runtime if a scene is added to the project (File->Build Settings).

    Unity throws an error if Application.LoadLevel() is given an invalid scene name (a scene not added to the project). How can I catch this error in JavaScript?

    ~ce
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Apparently you can't use try/catch in this case, so...not sure....

    --Eric
     
  3. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    848
    I wonder if I could somehow access the list shown in Build Settings...
     
  4. Shannon

    Shannon

    Joined:
    Dec 20, 2007
    Posts:
    21
    CarlEmail,

    Do you want to check to see if the level exists, without actually loading the level? If you are willing to load the level, of course

    Code (csharp):
    1.  
    2. function LoadNext () {
    3.     yield WaitForSeconds(2) ;
    4.     try {
    5.        Application.LoadLevel(2);
    6.     }
    7.     catch ( err ) {
    8.         Debug.Log("Load level error: "+err) ;  
    9.     }
    10. }
    11.  
    Isn't it not possible to compile if a script references a scene that does not exist in the Build Settings scene list?
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Unfortunately that code isn't going to work for a couple of reasons: 1) You can't use try/catch in a coroutine (using yield turns the function into a coroutine). 2) As I said earlier, try/catch won't work with Application.LoadLevel.

    --Eric
     
  6. Shannon

    Shannon

    Joined:
    Dec 20, 2007
    Posts:
    21
    Yep. I missed the point. Hopefully what I read this morning is to the point: "In your scripts you simply have to query if the level you are about to load has already been downloaded completely. By ensuring your first few levels are not using a lot of assets, you can start your game with only a minimal amount of data." This from the feature improvements list.
     
  7. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    That has to do with web streaming, not basic error checking to make sure the scene is included in the build settings.

    -Jeremy
     
  8. shotgunfox

    shotgunfox

    Joined:
    Apr 20, 2009
    Posts:
    18
    I'm wondering about this too. I'd like my "choose level" icons to know which level they should load, from a variable on the icon script. However, I'd like to be able to catch it if an icon is created for a level that does not exist. So when I do something like this:

    Code (csharp):
    1.  
    2. void OnMouseDown() {
    3.      Application.LoadLevel("Level" + thisIconNumber.ToString());
    4. }
    5.  
    and the icon number exceeds the amount of existing levels, I can take care of it instead of failing. Anyone found a solution since 2008?
     
  9. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Yes, write an editor script that reads out all scene names and then write that to a text file you load as text asset and only offer those scenes to jump in at all.

    or provide said text file manually or through web

    thats the way to go
     
  10. JP89

    JP89

    Joined:
    Sep 4, 2012
    Posts:
    2
    Solution incorrect.
     
    Last edited: Nov 7, 2012
  11. znoey

    znoey

    Joined:
    Oct 27, 2011
    Posts:
    174
    Solution incorrect?
    so doing something like this in an editor file:
    Code (csharp):
    1.  
    2.         string [] scenes =  new string[EditorBuildSettings.scenes.Length];
    3.         int count = 0;
    4.         foreach(var thing in EditorBuildSettings.scenes)
    5.         {
    6.             scenes[count] = thing.path;
    7.             ++count;
    8.         };
    9.         return scenes;
    10.  
    Then saving that as an asset to be used later doesn't work?
     
  12. jasonjeagerwolf

    jasonjeagerwolf

    Joined:
    Jul 2, 2012
    Posts:
    4
    The only way I've ever been able to detect this case is with something like this:

    Code (csharp):
    1.  
    2. Application.LoadLevel(name);
    3.  
    4. if (!Application.isLoadingLevel)
    5. {
    6.   Debug.LogError("Failed to load scene: " + name);
    7. }
    8.  
     
  13. cowlinator

    cowlinator

    Joined:
    Mar 15, 2012
    Posts:
    69
    Thanks Mr. Wolf!
     
  14. baci

    baci

    Joined:
    Apr 29, 2013
    Posts:
    4
    For anyone still interested in this, I catch this issue by checking Application.CanStreamedLevelBeLoaded(levelName)
     
    Magnesium and mgmhunt like this.
  15. phocker

    phocker

    Joined:
    Sep 12, 2010
    Posts:
    57
    Works! Thanks!
     
  16. FIFTYTWO

    FIFTYTWO

    Joined:
    Oct 21, 2014
    Posts:
    47
    Thank you! I was looking for this so long and you saved my time! It just works!
     
  17. Fred68to

    Fred68to

    Joined:
    May 27, 2017
    Posts:
    1
    Great ! Thanks, it solved the same problem for me...
     
  18. Forrest_Gimp

    Forrest_Gimp

    Joined:
    Jul 11, 2012
    Posts:
    4
    Thanks a thousand times from me as well. This was driving me nuts! Now if someone would care to explain to me why a straight forward try/catch is deliberately prevented from working here?
     
  19. Magnesium

    Magnesium

    Joined:
    Sep 14, 2014
    Posts:
    178
    Thanks. How isn't there none hacky way to do this???