- Home /
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);
}
}
}
Answer by herDev · Mar 21, 2017 at 07:47 AM
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!
So, I am going to have a LOT of scenes in my game, and I was just trying to streamline the process a bit with the dragging of the scene onto the public variable. I tried just using a string ins$$anonymous$$d and it works (thank you!), but I guess now I'm just left wondering why the scene object wouldn't work. Lol.
Answer by Adam-Mechtley · Mar 22, 2017 at 01:05 PM
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.