Search Unity

UnityEngine.Application.LoadLevel(int) is obsolete

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

  1. mtkopone

    mtkopone

    Joined:
    Mar 6, 2016
    Posts:
    1
    I'd say some of the hate is because at least I personally spent 20 minutes reading all the WRONG answers in this thread and trying them out before getting my javascript to work with "import UnityEngine.SceneManagement;".
     
  2. waqasnaeemalik

    waqasnaeemalik

    Joined:
    Mar 14, 2016
    Posts:
    1
    i have written this code for flappy bird but scenemanager.loadscene is not working on collision.how to fix this?


    using UnityEngine;
    using System.Collections;
    using UnityEngine.SceneManagement;

    public class bluebird : MonoBehaviour {

    [SerializeField]
    private Rigidbody2D mybluebird;

    [SerializeField]
    private float bouncespeed = 2f;
    [SerializeField]
    private float farwardspeed = 0.075f;

    [SerializeField]
    private Animator bluebirdanimation;

    public AudioSource birdsound;

    public AudioClip birdflap;
    public AudioClip birddied;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
    if (Input.GetKeyDown (KeyCode.A)) {
    mybluebird.velocity = new Vector2 (farwardspeed, bouncespeed);
    bluebirdanimation.SetTrigger ("flap");
    birdsound.PlayOneShot (birdflap);
    }

    }
    void FixedUpdate (){
    Vector2 temp = transform.position;
    temp.x = temp.x + farwardspeed;
    transform.position = temp;
    }

    void onCollisionEnter2D(Collision2D c)
    {
    SceneManager.LoadScene ("GameOver");
    }




    }
     
  3. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    Firstly you should capitalise the first O in onCollision as onCollision is not the same as OnCollision.
    Unity won't recognize onCollision as one of it's built in functions and won't trigger when something collides.
    You should add an if statement in the OnCollisionEnter2D function to have something to test against first.

    Also as a side note, it's good to remove any functions that are empty since Unity will check them anyway, even if empty. Unity will go through Start() even if it's empty as far as i'm aware.
     
  4. Solokeh

    Solokeh

    Joined:
    Dec 11, 2014
    Posts:
    2
    How in the world do we dynamically load the NEXT level with this new system? I've been searching, but there's nothing for it in the documentation or the forums.
     
  5. ShokeR0

    ShokeR0

    Joined:
    Feb 24, 2016
    Posts:
    112
    It's not the thread it is you.
    It took you 20 minutes to realize you are coding in the wrong language...
     
  6. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    How could I update this ?
    Code (CSharp):
    1.     if (score >= 30000&&Application.loadedLevelName=="1.2")
    2.         {
    3.            
    4.             Livello2.enabled = true;
    5.             Livello2.GetComponent<Image>().color = Color.white;
    6.             Lev2=true;
     
  7. ShokeR0

    ShokeR0

    Joined:
    Feb 24, 2016
    Posts:
    112
    Look at the Scenemanager doc
     
  8. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    Already tried but It does't work
     
  9. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    Code (CSharp):
    1. SceneManager.GetActiveScene().name=="1.1"
    It says that it doesn't exist the scene manager ,I ve added
    Code (CSharp):
    1. using UnityEngine.SceneManager
     
  10. Zeyav

    Zeyav

    Joined:
    Mar 21, 2016
    Posts:
    13
    You forgot to add ; after SeceneManager. using UnityEngine.SceneManager;
     
  11. itsjustoneguy

    itsjustoneguy

    Joined:
    Jun 21, 2015
    Posts:
    8
    for the javascript guys the line at the top needs to be

    import UnityEngine.SceneManagement;
     
  12. nmarinaro50

    nmarinaro50

    Joined:
    May 1, 2016
    Posts:
    1
    Hey Guys... I'm having an issue still with all this. I have Unity 5. No Matter what I try it doesn't recognize UnityEngine.SceneManagement. I am trying to us C# to make a simple start menu. The only thing the code is recognizing is Application.LoadLevel(1); but unity doesn't want to use that either. Can anyone help?? Here's the code that I have:

    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;

    public class NewBehaviourScript : MonoBehaviour {

    public Canvas exitMenu;
    public Button startText;
    public Button exitText;


    //Usethisforinitialization
    void Start ()

    {
    exitMenu = exitMenu.GetComponent<Canvas> ();
    startText = startText.GetComponent<Button> ();
    exitText = exitText.GetComponent<Button> ();
    exitMenu.enabled = false;

    }

    public void ExitPress()
    {
    exitMenu.enabled = true;
    startText.enabled = false;
    exitText.enabled = false;
    }

    public void NoPress()
    {
    exitMenu.enabled = false;
    startText.enabled = true;
    exitText.enabled = true;
    }

    public void StartGame()
    {
    Application.LoadLevel (1);
    }

    public void ExitGame()
    {
    Application.Quit ();
    }


    }
     

    Attached Files:

  13. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    You need to use
    Code (JavaScript):
    1.  
    2. using UnityEngine.SceneManagement;
     
  14. Jaconelli92

    Jaconelli92

    Joined:
    May 15, 2016
    Posts:
    1

    Thanks. im new to this, i was putting
    SceneManager.LoadScene(main);
    instead of
    SceneManger.LoadScene(1);
     
  15. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    If you want to load a scene by name, you should be using " ", like "main"
     
  16. Alifyz

    Alifyz

    Joined:
    May 28, 2016
    Posts:
    4
    I was using Application.loadedlevel() for getting a index and then using it for switch for the next level.
    For example, I was using like this.
    Code (CSharp):
    1. Application.LoadScene(Application.loadedlevel + 1);
    2.  
    And now, if you want to do the same thing you need to code like this.
    Code (CSharp):
    1. SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex + 1);
     
  17. Indra_O

    Indra_O

    Joined:
    May 9, 2017
    Posts:
    2
    I use this for my 3D game but the lightning that was automaticaly added by Unity dissapears when I use SceneManager.LoadScene
     
  18. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    763
    It only does weird lighting in editor (on scene change), if you do a build the lighting is as you want it to be.
     
  19. jmmota

    jmmota

    Joined:
    Aug 20, 2017
    Posts:
    1
    this is my soluction

    in Java

    import UnityEngine.SceneManagement;

    #pragma strict

    function OnGUI () {
    if (GUI.Button (Rect (Screen.width/2-50, Screen.height/2, 100, 30), "Jogar")) {
    SceneManager.LoadScene(1); //Application.LoadLevel
    }
     
  20. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    81
    Sorry if I 'bumped' any old posts or stepped on any toes, but I don't post unless I've noticed a large amount of people currently having a problem with the same issues. Even an old issue like the Application.LoadLevel becoming obsolete is still relevant in 2017 (2018 now) because, sadly there are still tons of documentation and tutorials teaching obsolete or inefficient scripting in Unity, even Unity's own documentation and tutorials haven't been completely updated. So, people (and moderators) please don't be so quick to close or condemn a post. Instead of commenting how a post is in the wrong section or not formatted properly, maybe just help out and answer it, not matter how old. It's sad to see a question go unanswered for over 5 years or more... :)

    So, to answer the question of how to use SceneManager.LoadScene properly in JAVA:

    1. Declare using IMPORT not C#'s USING (Mine worked fine when I removed #pragma strict)
    import UnityEngine.SceneManagement;

    2. It is better to use scene names to refer to your scenes (like "Room1") instead of integers because the assigned order of the scenes can change when adding/removing scenes, but scene names will always stay the same. So, to declare the string:
    public var loadLevel : String = "Room1"; // Replace 'Room1' with the name of your scene.

    3. Now when you want to load a scene in JS:
    SceneManager.LoadScene(loadLevel);

    There you go, for anyone still needing that...tested and confirmed.
     
    Last edited: Jan 27, 2018
  21. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    81
    Just to help out but in java you use "import" instead of "using" to add the SceneManager. I wrote the full instructions how to use SceneManager.LoadScene in java elsewhere on this page, but again it's:
    import UnityEngine.SceneManagement; //Add at very start of JS script
    public var loadLevel : String = "myScene"; //Replace myScene with your scene's name
    //then when you need to call it...
    SceneManager.LoadScene(loadLevel);
     
    Last edited: Jan 27, 2018
  22. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264

    This is a great answer.
    It helped! This error message was driving me nuts.. As a beginner its Easier said then done to when Unity themeself explain the change.. It made no sense for me :p
     
  23. N00MKRAD

    N00MKRAD

    Joined:
    Dec 31, 2013
    Posts:
    210
    You better switch to C# soon, JS is officially dead and no longer supported.

    Also, you just bumped a half year old thread.
     
  24. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    Nothing wrong with bringing up a still relevant topic, but yes I do agree the poster should move on to C# no point investing more time in something that will be dropped.
     
  25. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    Im using C#
    Im a beginner and when Unity make changes its difficult to figure out how to fix them... Since Im relying on Tutorials up top this point :p