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

SceneManager.LoadScene not working.

Discussion in 'Scripting' started by Zacocast, Jan 3, 2016.

  1. Zacocast

    Zacocast

    Joined:
    Jun 20, 2015
    Posts:
    10
    I've read all the current threads about this one. I'm simply trying to switch to the next scene in my game with this script:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. using System.Collections;
    4.  
    5. public class MenuManager : MonoBehaviour {
    6.     public void NewGame () {
    7.         SceneManagement.SceneManager.LoadScene(1);
    8. }
    9. }
    but I keep on getting error messages saying:
    All the other threads I read said that you have to have that SceneManagement namespace included to switch scenes, but even when I take it out the script doesn't work. When I do take it out it says:
    It was one of the other threads that I read that said that I had to have 'SceneManagement' before 'SceneManager' in this line. But even if I try to take 'SceneManagement' out here it just targets 'SceneManager' instead.

    I also realize that some people prefer to load their scenes using the scene's string name, but I just want to load my scene with its build index number, plain and simple. I'm wanting to use this script so I can make a menu button and assign it to this function, so that when I click it the script runs and directs the player to the next scene. Any help would be appreciated, thanks!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Since you imported the SceneManagement namespace, you don't specify SceneManagement when using SceneManager, since otherwise what you're writing is the equivalent of SceneManagement.SceneManagement.SceneManager. If you want to write out SceneManagement every time, then you wouldn't import that namespace.

    --Eric
     
  3. Zacocast

    Zacocast

    Joined:
    Jun 20, 2015
    Posts:
    10
    Good to know. I tried taking namespace off and leaving the SceneManagement on before the SceneManager though, and my script still gets an error saying that SceneManagement doesn't exist in the current context.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Because you'd need to specify UnityEngine.SceneManagement.

    --Eric
     
  5. Lentaq

    Lentaq

    Joined:
    Apr 8, 2015
    Posts:
    57
    Eric means without the import you would literally have to type:

    UnityEngine.SceneManagement.SceneManager.LoadScene(1);
     
    ItayMadeIt likes this.
  6. Zacocast

    Zacocast

    Joined:
    Jun 20, 2015
    Posts:
    10
    Like this?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MenuManager : MonoBehaviour {
    5.     public void NewGame () {
    6. UnityEngine.SceneManagement.SceneManager.LoadScene (1);
    7. }
    8. }
    I still get the error response about SceneManagement not being included in the UnityEngine namespace. I'm sorry about my misunderstanding, I'm more used to JavaScript and I guess namespaces just haven't clicked with me yet.
     
    KingNullPointer and ALEX_PAINT like this.
  7. Lentaq

    Lentaq

    Joined:
    Apr 8, 2015
    Posts:
    57
    Zaco, I looked and this might be an issue with which version of Unity you are using. This was added in the 5.3 update. So, if you are using an older version, you would use Application.LoadLevel().

    I haven't actually upgraded mine to 5.3, so I can't test or mess around with that atm. See if you are using the right call for the right version.

    Check this link for comments on it.
     
    Zacocast likes this.
  8. Airvant

    Airvant

    Joined:
    Jan 4, 2016
    Posts:
    1
    Add below code on top of your script.
    using UnityEngine.SceneManagement;
     
  9. Zacocast

    Zacocast

    Joined:
    Jun 20, 2015
    Posts:
    10
    Ok, this is exactly what I needed, thank you so much. Also, sorry for my late reply. It was kind of weird because I even went into the Check For Updates menu in Unity and it claimed that I was up to date with 5.2.1 or something like that. I had to look on Unity's website myself to find it. My code works perfectly now. Thanks again for your help.
     
  10. Aerial_Guardian

    Aerial_Guardian

    Joined:
    Feb 11, 2017
    Posts:
    5
    Has it changed again? What is it now? Neither (Application.LoadLevel1); or ceneManagement.SceneManager.LoadScene(l); work for me. I get told to use SceneManagement, then I do and then Im told Assets/Standard Assets/Utility/TimedObjectActivator.cs(85,13): error CS0103: The name `SceneManagement' does not exist in the current context.

    Other peoples answers are very vague too. Know what's up?

    EDIT: NVM, fixed it.

    Code (csharp):
    1. SceneManagement.SceneManager.LoadScene(1);
    should be

    Code (CSharp):
    1. SceneManager.LoadScene(1);
    for Error: Assets/Standard Assets/Utility/TimedObjectActivator.cs(85,13): error CS0103: The name `SceneManagement' does not exist in the current context.
     
    Last edited: Feb 18, 2017
  11. chitifatimah

    chitifatimah

    Joined:
    Dec 5, 2016
    Posts:
    1
    I have a problem about this, can you help me?
     

    Attached Files:

  12. SoshJam

    SoshJam

    Joined:
    Dec 23, 2016
    Posts:
    8
    What is the problem?
     
  13. KHRJ

    KHRJ

    Joined:
    Dec 13, 2016
    Posts:
    1
    I had the same problem. Fixed it, so now it works using the name of the scene! yey!
    Here is my ugly code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;

    public class House : MonoBehaviour {

    public Text houseText;

    void Start()
    {
    houseText.gameObject.SetActive(false);
    }

    private void OnTriggerStay2D(Collider2D other)
    {
    if (other.CompareTag("Player"))
    {
    Debug.Log("Player is still touching");
    houseText.gameObject.SetActive(true);

    if (Input.GetKeyDown("e"))
    {
    SceneManager.LoadScene("InsidePotatoHouse");
    }

    }
    }
    private void OnTriggerEnter2D(Collider2D other)
    {
    if (other.CompareTag("Player"))
    {
    Debug.Log("Player Entered");
    houseText.gameObject.SetActive(true);
    }
    }
    private void OnTriggerExit2D(Collider2D other)
    {
    if (other.CompareTag("Player"))
    {
    houseText.gameObject.SetActive(false);
    Debug.Log("Player Left");
    }
    }
    }
     
  14. mnuwor

    mnuwor

    Joined:
    Oct 27, 2019
    Posts:
    1
    using UnityEngine;
    using UnityEngine.SceneManagement;
    public class SceneManager : MonoBehaviour
    {
    public int Buildindex;

    public void Restart()
    {//Restart current scene

    SceneManager.LoadScene(SceneManager.GetActiveScene().name);


    }
    void Levelselect()
    {

    }


    }


    please help
     
  15. Peytoncl

    Peytoncl

    Joined:
    Jun 12, 2020
    Posts:
    2
    i know this is very old but you names your class the same thing as scenemanager.
     
  16. crealom

    crealom

    Joined:
    Jun 5, 2020
    Posts:
    2
    This may be an old thread, but I found the fix was to call LoadScene() during an Update() call rather than a different thread.
     
    MilanCREEPERPOWER likes this.