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

UnityEngine.Application.LoadLevel(int) is obsolete

Discussion in 'Scripting' started by Brandon-Keeler, Dec 9, 2015.

  1. Brandon-Keeler

    Brandon-Keeler

    Joined:
    Oct 27, 2014
    Posts:
    75
    I am getting many errors like the following within my project after upgrading to unity 5.3

    The problem is when I try to change it, it comes up with the error
    Can someone tell me what im doing wrong because it thinks
    Code (CSharp):
    1. SceneManager.LoadScene
    doesn't exist but its telling me to use it.

    What I previously would have done was
    Code (CSharp):
    1. Application.LoadLevel(2);
     
    Last edited: Dec 9, 2015
  2. Yeoli_Livunts

    Yeoli_Livunts

    Joined:
    Jul 20, 2015
    Posts:
    7
    Same issues here. Please help!!
    here my error:

    warning CS0618: `UnityEngine.Application.LoadLevel(int)' is obsolete: `Use SceneManager.LoadScene'

    I changed it and got the same error as OP!

    Edit: Also it keeps crashing during testing. I am filing an error report
     
    Deleted User and Myzer like this.
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Yeoli_Livunts likes this.
  4. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    add
    using UnityEngine.SceneManagement;

    to the top of your script than use SceneManager.LoadScene(string scenePath)
     
  5. Yeoli_Livunts

    Yeoli_Livunts

    Joined:
    Jul 20, 2015
    Posts:
    7
    Thank you. Will do this now. Sorry, I'm still learning. Thank you so much!
     
    Myzer likes this.
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    we all are scenemanager is new in 5.3 :D
     
    Myzer and RavenMikal like this.
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Ugh. Sounds like its time for me to go read the change log. What else did they break?
     
  8. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    You can still use the old OpenScene stuff from Application but it is deprecated in favour of the scene manager
     
    theDrake and Myzer like this.
  9. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,965
    Last edited: Dec 10, 2015
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Just read through the release notes. Not that much actually broke.
     
    Ryiah and Myzer like this.
  11. puppeteer

    puppeteer

    Joined:
    Sep 15, 2010
    Posts:
    1,282
    The automatic API updater is a real timer-saver for people who are keeping several versions of a package.

    I wonder why they hadn't added things like Application.LoadLevel() --> SceneManager.LoadScene() to the auto-update list, it seems like a simple enough change. I don't see them in the non-auto-update list either. Did they forget it or something?
     
    Myzer likes this.
  12. AGaming

    AGaming

    Joined:
    Dec 26, 2013
    Posts:
    103
    But how to do this in Java?
     
  13. puppeteer

    puppeteer

    Joined:
    Sep 15, 2010
    Posts:
    1,282
    In JS you write at the top:

    Code (JavaScript):
    1. import Unity.SceneManagement
    After that you can replace Application.LoadLevel() with SceneManager.LoadScene()
     
    RuneShiStorm likes this.
  14. Mo-The-Black-Cat

    Mo-The-Black-Cat

    Joined:
    Dec 15, 2015
    Posts:
    2
    Sorry i'm fairly new at this, the import line return me the following error: "BCE0021: Namespace 'Unity.SceneManagement' not found, maybe you forgot to add an assembly reference?".

    I guess I did something wrong?
     
  15. mikeymike

    mikeymike

    Joined:
    Oct 21, 2014
    Posts:
    35
    try
    Code (csharp):
    1. using UnityEngine.SceneManagement;
     
  16. Mo-The-Black-Cat

    Mo-The-Black-Cat

    Joined:
    Dec 15, 2015
    Posts:
    2
    Yes I just figured it out at the moment ha ha, thank you very much anyway.
     
  17. AGaming

    AGaming

    Joined:
    Dec 26, 2013
    Posts:
    103
    I put it into the code, but I get the error:
    UCE0001: ';' expected. Insert a semicolon at the end.
     
  18. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    What happens when you put semicolons in all the right places?
     
  19. AGaming

    AGaming

    Joined:
    Dec 26, 2013
    Posts:
    103
    Code (JavaScript):
    1. import Unity.SceneManagement
    2. #pragma strict
    3.  
    4.  
    5. function Start () {
    6.  
    7.     SceneManager.LoadScene("Continue");
    8.    
    9. }
    I'm trying so here. I do not see where you can Postan semicolon.
     
  20. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Semicolons go at the end of statements. Like they always have.

    This includes your import statement on line 1

    Code (JavaScript):
    1. import Unity.SceneManagement;
    2. #pragma strict
    3.  
    4.  
    5. function Start () {
    6.  
    7.     SceneManager.LoadScene("Continue");
    8.  
    9. }
     
  21. AGaming

    AGaming

    Joined:
    Dec 26, 2013
    Posts:
    103
    If I do as you have described here I get this error.

    BCE0021: Namespace 'Unity.SceneManagement' not found, maybe you forgot to add an assembly reference?
     
  22. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The answer is already in this thread about three posts ago...

     
  23. puppeteer

    puppeteer

    Joined:
    Sep 15, 2010
    Posts:
    1,282
    Sorry, I missed the semicolon at the end of the line. It should be like this:

    Code (JavaScript):
    1. import Unity.SceneManagement;
    2.  
     
  24. PimajeXenja

    PimajeXenja

    Joined:
    Dec 31, 2014
    Posts:
    10
    I get the "The type or namespace name `SceneManagement' does not exist in the namespace `UnityEngine'. Are you missing an assembly reference?" error even when putting "using UnityEngine.SceneManagement;" at the top of my CS script.
     
  25. puppeteer

    puppeteer

    Joined:
    Sep 15, 2010
    Posts:
    1,282
    Can you put your script here?
     
  26. mikeymike

    mikeymike

    Joined:
    Oct 21, 2014
    Posts:
    35
    if you type UnityEngine.SceneManagement; as opposed to Unity.SceneManagement, it will work.
     
  27. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    I had the same mixup with my JS script. I couldn't find that "import Unity.SceneManagement" anywhere though :p
    I added
    Code (JavaScript):
    1. import Unity.SceneManagement;
    at the top of the script, but it then gives me this: "Namespace 'Unity.SceneManagement' not found, maybe you forgot to add an assembly reference?"

    *EDIT*
    It should be UnityEngine.SceneManagement, not Unity.Scene...
    This fixed it for me.
     
    Chris-Ramer likes this.
  28. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    It's amazing how people jump to conclusions without trying it.

    Application. LoadLevel is deprecated, and still works. It just throws a warning if you do so. Both the new scene manager and loadlevel stuff still exists in the docs, and vs will tell you Application.LoadLevel is deprecated right in the intellisense
     
    Kiwasi likes this.
  29. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    Yeah, Application.Load still works, it's just good practice to change stuff that are marked as obsolete as they may get removed completely down the line.
     
  30. samuelhavel

    samuelhavel

    Joined:
    Dec 10, 2012
    Posts:
    3
    How can I do this?
    Code (CSharp):
    1. if (Application.loadedLevelName == "SceneName")
    2. {
    3. ...
    4. }
     
    Tutterzoid likes this.
  31. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    SceneManager.GetActiveScene().name
     
  32. samuelhavel

    samuelhavel

    Joined:
    Dec 10, 2012
    Posts:
    3
    Thank you!

    Everything was working, now my android app is closing soon after opening. Unity 5.3 is hindering the work! :mad:
     
  33. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    I also had that, the app shutting down right after it started.
    I actually just switched from mono to il2cpp and it worked for some odd reason.
     
  34. johanbrodd

    johanbrodd

    Joined:
    Dec 25, 2015
    Posts:
    1
    As a new unity user I feel that the manual here is rather useless. And that's to put it at it's mildest form;

    http://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html

    They say one have to use SceneManager.LoadScene(), but have totally neglected to provide a working script to look at. This is after all a manual? Shouldn't it teach the users how to do things properly? This scares people who got limited expertise away.
     
    Muckel likes this.
  35. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You are reading the scripting api, not the manual. Its slightly different.

    What information do you think is missing? An example would almost be to trivial to bother with. But here is what it might look like.

    Code (CSharp):
    1. // Load the level when the button L is pushed
    2.  
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class SceneLoader : MonoBehaviour {
    7.     void Update {
    8.         if(Input.GetKeyUp(Keycode.L){
    9.             SceneManger.LoadScene(1);
    10.         }
    11.     }
    12. }
     
  36. PimajeXenja

    PimajeXenja

    Joined:
    Dec 31, 2014
    Posts:
    10
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. using System.Collections;
    4.  
    5.  
    6. public class ButtonController : MonoBehaviour {
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.    
    16.     }
    17.  
    18.     public void StartGame ()
    19.     {
    20.         Application.LoadLevel("Menu_Select");
    21.     }
    22.  
    23.     public void ExitGame ()
    24.     {
    25.  
    26.     }
    27.  
    28.     public void PlainsLevel ()
    29.     {
    30.         Application.LoadLevel("Ace");
    31.     }
    32.  
    33.     public void BeachLevel ()
    34.     {
    35.  
    36.     }
    37.  
    38.     public void AntarticLevel ()
    39.     {
    40.  
    41.     }
    42.  
    43.     public void DesertLevel ()
    44.     {
    45.  
    46.     }
    47.  
    48.     public void ExitToMainScreen ()
    49.     {
    50.         //SceneManager.loadScene("Menu_Screen_Final");
    51.         Application.LoadLevel("Menu_Scene_Final");
    52.         Time.timeScale = 1.0f;
    53.     }
    54.  
    55.     public void RestartLevel ()
    56.     {
    57.         Application.LoadLevel(Application.loadedLevel);
    58.     }
    59. }
    60.  
     
    Milekic likes this.
  37. puppeteer

    puppeteer

    Joined:
    Sep 15, 2010
    Posts:
    1,282
    @PimajeXenja:

    I took your code and updated it to use SceneManager.LoadScene, and it works just fine:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. using System.Collections;
    4.  
    5.  
    6. public class ButtonController : MonoBehaviour
    7. {
    8.  
    9.     // Use this for initialization
    10.     void Start()
    11.     {
    12.  
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.  
    19.     }
    20.  
    21.     public void StartGame()
    22.     {
    23.         SceneManager.LoadScene("Menu_Select");
    24.     }
    25.  
    26.     public void ExitGame()
    27.     {
    28.  
    29.     }
    30.  
    31.     public void PlainsLevel()
    32.     {
    33.         SceneManager.LoadScene("Ace");
    34.     }
    35.  
    36.     public void BeachLevel()
    37.     {
    38.  
    39.     }
    40.  
    41.     public void AntarticLevel()
    42.     {
    43.  
    44.     }
    45.  
    46.     public void DesertLevel()
    47.     {
    48.  
    49.     }
    50.  
    51.     public void ExitToMainScreen()
    52.     {
    53.         //SceneManager.loadScene("Menu_Screen_Final");
    54.         SceneManager.LoadScene("Menu_Scene_Final");
    55.         Time.timeScale = 1.0f;
    56.     }
    57.  
    58.     public void RestartLevel()
    59.     {
    60.         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    61.     }
    62. }
    Are you using Unity 5? There is no UnityEngine.SceneManagement; in Unity 4.
     
    Milekic likes this.
  38. AndrewBilotti

    AndrewBilotti

    Joined:
    Jan 6, 2016
    Posts:
    3
    I REALLY do not like how now application.loadLevel is now obsolete. I don't care though.
     
    Guhanesh and Muckel like this.
  39. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    What an incredibly stupid naming scheme from Unity.
     
  40. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,965
    It's better than their old style of calling them "levels" in one location and "scenes" in another. :p
     
  41. JohannChristoph

    JohannChristoph

    Joined:
    Jun 21, 2009
    Posts:
    141
    for the Javascript-folks again: as mikeymike said: if you add this line in the beginning it works.
    Code (JavaScript):
    1.  import UnityEngine.SceneManagement;
     
    softwizz likes this.
  42. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    Honestly not sure why all the hate. It's a better API(although I wish Unity would follow OOP and give us a "UnityGame" instance instead of a static reference) and allows a lot more control with multiscene editing. It's a very trivial issuers upgrade, however it should be made more evident to a beginner of where they should go for help.
     
    Ryiah likes this.
  43. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    Perhaps the hate is from that there is no clear documentation for those using UnityScript.
    I don't remember saying anywhere to use import UnityEngine.Scenemanagement. (Dunno if they changed it now, i did suggest it when they added the manager.)
     
  44. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    You can clearly see in the docs it is in the UnityEngine.SceneManagmeant namespace so why does it need to be spelled out?
     
  45. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    When i first used that, i was using JS and i never had to use import.
    I wasn't aware of that even. I did switch over to C# btw, but we were expecting js to work for the most part.
    So yeah, lots of people that were in js weren't aware of the namespace import and there were threads of people asking why it's not working even if they are using it exactly as it was documented.
     
  46. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    so you have never used any of the Event interfaces the UI system or UnityEvents or wrote a editor script?
    All of these are in their own namespaces that you would need to import
     
  47. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    I have. The UI. And not all are power users. Documentation should be complete, that's why you have it there. Not saying "yeah they'll remember from before".
    Some are just learning now.
    The example scripts in there forthe UI had that import, the ones for scene manager did not, while they were complete for C#.
    I know they are trying to drop UnityScript in the end, but it's not there yet.
     
  48. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    neither one actually shows a example, both the c# and JavaScript one show the just the methods signature with some text explaining what each argument is for and what the method does.

    My point was regardless of if your looking at the JavaScript or the C# version of any class or method it does display what namespace that class is a apart of in the sidebar.
    http://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html

    knowing how to browse the docs is kinda a needed skill and namespaces are a thing if you want to do most things. The only issue about this i see is that in JS you dont have to be explicit about the UnityEngine namespace like you do in c# so people aren't exposed to it on their first script
     
    Last edited: Feb 4, 2016
    landon912 likes this.
  49. jesusluvsyooh

    jesusluvsyooh

    Joined:
    Jan 10, 2012
    Posts:
    377
    Thanks for all the links and Info, just starting to update my 'old' Unity 4 games to 5.

    You can see most of them here, i have over 2 million downloads in total now ^_^ Thanks Unity team and community! :D
    www.stephenallengames.co.uk/games.php

    Edit: And after an hour of opening, converting, updating api/yellow warnings and red errors i've just noticed i did it on the wrong project folder lol start again :eek:
     
    Last edited: Feb 27, 2016
  50. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    WOW you solved my issue, couldnt get this to work in JavaScript because the scripting api says:
    And this obviously does not work.

    http://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html