Scenes won't change when built

I am using UnityEditor.SceneManagement to change my scenes, and it works perfectly in editor but they won’t change in game. Help please.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.SceneManagement;
public class portal2 : MonoBehaviour {

[SerializeField] private string level;

void OnTriggerEnter(Collider other) 
{
	if (other.CompareTag("Player"))
		{
		
		EditorSceneManager.LoadScene (level);
		}
}

}

Fixed it. I was using UnityEditor.SceneManagement instead of UnityEngine.SceneManagement.
Turns out UnityEditor only works in the editor. Also use SceneManager.LoadScene instead of EditorSceneManager.LoadScene.

Hi !
I recently got the same problem. It had just a twist in my case, the problem came from the Unity docs that were using this piece of code to retrieve the name of an Asset passed in the editor’s inspector :

#if UNITY_EDITOR
    public UnityEditor.SceneAsset SceneAsset;
    private void OnValidate()
    {
        if (SceneAsset != null)
        {
            m_SceneName = SceneAsset.name;
        }
    }
#endif

And so the name would be accessed only when the code ran into the editor and not when built.
I hope this may helkp someone else :slight_smile: