SceneManager.LoadScene(sceneName) not working in build

I use the code below to switch to a new scene when the player enters the (Trigger) BoxCollider2D of the Gameobject represents the end of the level.

The ‘nextLevel’ object is the next scene, which I drag onto the Inspector.

(I know, you can drag any object into this field, I have to check if its a scene that can be loaded).

This code works perfectly fine in the Editor, however once I build the game, this doesnt work at all.

I have to use ‘SceneManager.LoadScene(BuildIndex);’ for it to work.

[SerializeField] private Object nextLevel;

    public void WinLevel()
    {
        SceneManager.LoadScene(nextLevel.name);
    }

Is there a way to still have the levelDesigner just drag the next Scene onto the GameObject?

I don’t believe this is possible like this.

However, with some editor scripting, you can have the exact same behaviour.

Disclaimer

All the credits of the following scripts go to Stewart L. McCready

// SceneAttribute.cs

using UnityEngine;
/// <summary>
/// Attribute declaration for SceneDrawer, to be used on strings.
/// </summary>
public class SceneAttribute : PropertyAttribute {
}

// SceneDrawer.cs in a folder called `Editor` 

using UnityEditor;
using UnityEngine;

/// <summary>
/// Scene Drawer, strings tagged [Scene] now show Scene selection instead of strings.
/// <para/>
/// Use [SerializeField, Scene] to display theScene selection window instead of a string input box.
/// This property drawer will also check if the selected scene has already been added to 'Scenes in the Build'
/// if not it will warn the user.
/// Set strings to "" for the property drawer to correctly select 'none' without display error logs.
/// </summary>
[CustomPropertyDrawer(typeof(SceneAttribute))]
public class SceneDrawer : PropertyDrawer {

	/// <summary>
	/// Overrides Unity display of scenes tagged with [Scene].
	/// </summary>
	public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {

		// Only override rendering on properties with type of string (otherwise it doesn't work).
		if (property.propertyType == SerializedPropertyType.String) {
			// Get currently selected object (generated from previous string value).
			SceneAsset sceneObject = GetSceneObject(property.stringValue);
			// Render a SceneAsset object field, with the previous string value, shown as a SceneAsset.
			Object scene = EditorGUI.ObjectField(position, label, sceneObject, typeof(SceneAsset), true);

			if (scene == null) {
				// We get nulls if the scene asset isn't found so wipe the string value.
				property.stringValue = "";

				// If the scene name doesn't match the property they have changed it
				// either by drag n drop or the object picker.
			} else if (scene.name != property.stringValue) {
				// Convert the Object to a SceneAsset.
				SceneAsset sceneObj = GetSceneObject(scene.name);
				// If its a valid scene asset we use it, otherwise assume select invalid and ignore.
				if (sceneObj != null) {
					property.stringValue = scene.name;
				}
			}
		} else {
			EditorGUI.LabelField(position, label.text, "Use [Scene] with strings.");
		}
	}

	/// <summary>
	/// Retrieve the scene string from the editor build settings, returns null if not found.
	/// 
	/// </summary>
	/// <param name="sceneObjectName"> The asset name path of the scene object. </param>
	/// <returns> The SceneAsset or null if not found. </returns>
	protected SceneAsset GetSceneObject(string sceneObjectName) {
		if (string.IsNullOrEmpty(sceneObjectName)) {
			// Early exit as know it will return null when string null or empty.
			return null;
		}
		// Iterate over the scenes in the build settings
		foreach (EditorBuildSettingsScene editorScene in EditorBuildSettings.scenes) {
			// We found the scene object's name in the editor scene's path.
			// This assumes that a scene will not be named exactly the same as a parent folder & a duplicate of another scene.
			if (editorScene.path.IndexOf(sceneObjectName) != -1) {
				return AssetDatabase.LoadAssetAtPath(editorScene.path, typeof(SceneAsset)) as SceneAsset;
			}
		}
		// Scene not found, assume it isn't in the build settings.
		Debug.LogWarning("Scene [" + sceneObjectName + "] cannot be used. Add this scene to the 'Scenes in the Build' in build settings.");
		return null;
	}
}

Then, just add the [Scene]attribute to your serialized string.