Inspector defined variable wiped away when game is built.

I have a collider in my level, and when you touch it, it loads an assigned scene. The scene is assigned in the inspector by dragging the scene onto the public object variable. When I build the game, this variable that I had set in the inspector was said to be null (I did a dev build to figure that out).

My exit script is nothing special, but here it is:

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

public class ExitScript : MonoBehaviour {

public Object nextScene;
public string nextLevel;

public LevelManager levelManager;

void Start()
{
	nextLevel = nextScene.name;
	levelManager = FindObjectOfType<LevelManager> ();
}

void OnTriggerEnter(Collider other)
{

	if (other.tag == "Player") {
		
		levelManager.changingLevel = true;

		levelManager.ChangeLevel (nextLevel);

	}

}

}

Hi!

Rather than assigning the scene as an object in the inspector and taking it’s name from the object name, why not define the scene(s) by buildIndex int or sceneName string through code, maybe in your levelManager and load it with SceneManager.LoadScene (value). I assume levelManager.ChangeLevel method calls LoadScene at the moment too.

Depending on how many scenes you have in your project you could also use SceneManager.sceneCount to populate a list of scene variables too.

Hope that helps!

SceneAsset, which is what you are assigning to that object reference field, is a type in the UnityEditor namespace, and so it does not exist at run-time.