[Solved] SceneManager doesn't work

Hello everyone. I am using Unity 2017.3.0f3 and had to use SceneManager instead of Application.LoadLevel. I changed all my scripts to use SceneManager until there were no red squiggly lines and my game could run.
However, now that I switched to SceneManager the scripts dont work. The Play button and the Lose Collider no longer change scenes. In the script component I can no longer find the functions. Could someone help me with this?
Here is my code in case you want to see it:

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

public class SceneChange : MonoBehaviour
{
    static SceneChange Instance;

    public void LoadScene(string name)
    {
        Debug.Log("LevelLoad" + name);
        SceneManager.LoadScene("Start");
    }

    public void QuitRequest()
    {
        Debug.Log("Quit");
        Application.Quit();
    }

    public void GetActiveScene()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }
}

So I’ve not use the newer version of Unity yet and have never touched the scene manager but according to the documentation

LoadScene takes 2 inputs. The scene to be loaded(either as an int or string) then the load mode. If you add “LoadSceneMode.Single” after the scene selection this may fix it.

SceneManager.LoadScene("Start",LoadSceneMode.Single);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex +1, LoadSceneMode.Single);
2 Likes

I assume the answer by @Martyn_Stewart is sufficient but since I am using the newer version of Unity, I’ll share my (working) code just in case. Hope this helps.

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


public class LevelManager : MonoBehaviour {
    public int buildIndex;

    public static int lives = 3;
    public static float time;

    // Everytime a new level is started
    public void LoadLevel(string name)
    {
       // Reset of brick counter to make sure they donĂ˝ accumulate and become hogher than max
       BrickHit.breakableChildrenCount = 0;

       SceneManager.LoadScene(name);
    }
        

    public void BrickDestroyed()
    {
        // When all breakable bricks are destroyed, you can go on to next level 
        if (BrickHit.breakableChildrenCount <= 0)
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
        }
    }

    // What happens when you hit the LoseCollider on the bottom
    public void DownTheDrain()
    {
        lives--;
        Debug.Log("The player has " + lives + " lives");

        // In case of game over
        if (lives <= 0)
        {
            lives = 3;
            SceneManager.LoadScene("Completed");
        }

        // In case player has lives left
        else
        {
            GameObject.FindObjectOfType<Lives>().DrawBallsforLives();
            GameObject.FindObjectOfType<Ball>().RestartLevel();
        }
        
    }
1 Like

Hello!
i am also using the newest version of Unity. From what I can see from your code, the methods to load the scenes using the SceneManager instead of the Application are correct.
However, in your LoadScene method, you are giving the “Start” as an argument instead of the variable string name.
In this case, your game will always go back to the Start scene whenever this method is called from any other scene even if you pass the correct names of the scenes as arguments.
Hope this help =)

2 Likes