Build error: The type or namespace name 'UnityEditor' could not be found.

When making my game, I use button, ads, etc. When I try to build this game it says “The type or namespace name ‘Unity Editor’ could not be found.” I have all of them referencing Unity Editor, and I cannot put them into a Editor folder because these scripts make the game work! it happens with all my scripts and I have no idea what to do please Help!

ADS SCRIPT:

TOUCHABLE SCRIPT:

PP SCRIPT:

Classes in the UnityEditor namespace are for use in editor scripts only.

When you create a build you need to remove the reference to that namespace and rewrite any code that uses classes from it.

First of all you can not use or reference anything from the UnityEditor namespace in a runtime script. If you do you have to ensure to wrap everything related to the UnityEditor namespace in pre-processor tags so it’s not included at runtime.

I don’t see any reason why your “Ads” script would require UnityEditor.Advertisement so just remove it.

Your “Touchable.cs” contains both, runtime code and editor code. This makes no sense. You have to move the Editor code into a seperate file which you place in an editor folder. The editor code can’t be executed at runtime and has no effect on the runtime class.

// Touchable.cs
using UnityEngine;
namespace UnityEngine.UI {
 
    public class Touchable : Graphic {
 
        public override bool Raycast(Vector2 sp, Camera eventCamera) {
            return true;
        }
 
        protected override void OnPopulateMesh(VertexHelper vh) {
            vh.Clear();
        }
    }
}

This goes into a seperate file which you place in an editor folder.

// TouchableEditor.cs
using UnityEngine;
using UnityEditor;
namespace UnityEngine.UI
{
    [CustomEditor(typeof(Touchable))]
    public class TouchableEditor : Editor
    {
        public override void OnInspectorGUI() {}
    }
}

Finally your “pp” script uses an editor class to change the current scene. Why is that method “changeScene” actually inside a runtime class? Since you wrapped it in pre-processor tags the method isn’t included in your build anyways. So it only works inside the editor. If you really need this you either have to wrap the using UnityEditor; and using UnityEditor.SceneManagement; as well in preprocessor tags so it’s not included in your build. However in cases where you only need to use a single editor class you better remove the using statement and use the full class name:

using UnityEngine;

public class pp : MonoBehaviour
{
    #if UNITY_EDITOR
    public void changeScene (int cs)
    {
        UnityEngine.SceneManagement.EditorSceneManager.LoadScene(cs);
    }
    #endif
}

However if you actually want to be able to change the level at runtime you just want to use UnityEngine.SceneManagement.SceneManager.LoadScene(sceneIndex); which is a runtime class.