Scene as a variable

Is there a way to set a scene as a variable?

You can use a scene as

using UnityEngine.SceneManagement;

public Object sceneToLoad;

and then load the scene / or whatever you want to do with it like

SceneManager.LoadScene(sceneToLoad.name);

Just storing a path string (as the SceneAsset documentation suggests) is inadequate for production, because if the scene file is renamed you’ve lost your reference. We can use an Object reference to the SceneAsset but we no longer have access to AssetDatabase to look up the path when not in editor.

To reliably handle both file renames and runtime paths you need two serialized pieces of data (Object reference in editor, string path at runtime). You can create a SceneReference object that uses ISerializationCallbackReceiver to ensure that the stored path is always valid based on the specified SceneAsset Object.

As an example, here is my approach to this problem: [Unity3D] A Reliable, user-friendly way to reference SceneAssets by script. · GitHub

alt text


Features

  • Custom PropertyDrawer that displays the current Build Settings status, including BuildIndex and convenient buttons for managing it with destructive action confirmation dialogues.
  • If (and only if) the serialized Object reference is invalid but path is still valid (for example if someone merged incorrectly) will recover object using path.
  • Buttons collapse to smaller text if full text cannot be displayed.
  • Includes detailed tooltips and respects Version Control if build settings is not checked out (tested with Perforce)

alt text

Scenes can generally be referred to by name (e.g. in Application.LoadLevel), so you can probably use a string variable to hold the name of the scene in question.

try something like this. You would likely want the level array to be visible in the inspector so you would want it outside of the function but for simplicity sake in showing the example I have it inside the function.

public void chooseLevel(int _levelArrayIdx)
	{
		string[] levels = new string[]{"Level1","Level2","Level3"};
		Application.LoadLevel(levels[_levelArrayIdx]);
	}

So this is an old post, but i figured people are still looking for an answer here. I recently came across this:

using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameStarter : MonoBehaviour
{
    public SceneAsset offlineScene;

    private void Awake()
    {
        if (GameObject.FindGameObjectWithTag("NetworkManager") == null)
            SceneManager.LoadScene(offlineScene.name);
    }
}

Just make sure you are using UnityEditor.
@Derpyness

using UnityEngine;
using UnityEngine.SceneManagement;

 public class SceneChanger : MonoBehaviour
 {     
     //Change Scene By Name
     public void ChangeScene(string name)
     {
          SceneManager.LoadScene(name);
     }
     //Change Scene By ID
     public void ChangeScene(int id)
     {
          SceneManager.LoadScene(id);
     }
 }

I don’t know how it works over network, probably doesn’t but this is the solution I’ve used since then.