When making Build And Run i'm getting two errors on my script but i can play the game help ?

I have now unity 5.4.1f1 personal

when running the game pressing play in editor not errors everything is fine.
When trying to make Build And Run standalone for pc i’m getting errors:

Error building Player because scripts had compiler errors

And

Assets/MyScripts/FullScreenPlayMode.cs(1,7): error CS0246: The type or namespace name `UnityEditor’ could not be found. Are you missing a using directive or an assembly reference?

And this is my script and i don’t see any errors built it again i can play the game through the editor this errors come up only when making Build And Run:

using UnityEditor;
using UnityEngine;
using System.Collections;

//[InitializeOnLoad]
public class FullScreenPlayMode : MonoBehaviour {

    //The size of the toolbar above the game view, excluding the OS border.
    private int tabHeight = 22;

    void Start()
    {
        FullscreenPlayMode ();
    }

    void FullscreenPlayMode()
    {
        EditorApplication.playmodeStateChanged -= CheckPlayModeState;
        EditorApplication.playmodeStateChanged += CheckPlayModeState;

    }

    void CheckPlayModeState()
    {
        if (EditorApplication.isPlaying)
        {
            FullScreenGameWindow();
        }
        else 
        {
            CloseGameWindow();
        }
    }

    EditorWindow GetMainGameView(){
        EditorApplication.ExecuteMenuItem("Window/Game");

        System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor");
        System.Reflection.MethodInfo GetMainGameView = T.GetMethod("GetMainGameView",System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
        System.Object Res = GetMainGameView.Invoke(null,null);
        return (EditorWindow)Res;
    }

    void FullScreenGameWindow(){

        EditorWindow gameView = GetMainGameView();

        gameView.titleContent.text = "Game (Stereo)";
        Rect newPos = new Rect(0, 0 - tabHeight, Screen.currentResolution.width, Screen.currentResolution.height + tabHeight);

        gameView.position = newPos;
        gameView.minSize = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height + tabHeight);
        gameView.maxSize = gameView.minSize;
        gameView.position = newPos; 

    }

    void CloseGameWindow(){
        EditorWindow gameView = GetMainGameView();
        gameView.Close();
    }

    void Update()
    {
        if(Input.GetKey("escape")) {//When a key is pressed down it see if it was the escape key if it was it will execute the code
            //CloseGameWindow(); // Quits the game
            Application.Quit();
        }
    }
}

Answer is in this link:

Basically UnityEditor does not exist in builds. All this code will only work in the editor but it will give you errors when you build it for another platform.

The solutions are on the link I gave you as well.

Cheers!

1 Like

Still not working.
Too many problems.

Let me explain what i did and what is not working.

After the first problem in my question i went to the link to try and solve it.
First thing i moved the script file to the directory Editor.

But since in my script i have the line: [InitializeOnLoad]
The game never get to the Update function and thereforce the Escape key can’t be pressed.
To make the Update function to work and the Escape key to work i need to attach the script to something for example to the ThirdPersonController.

But since i moved the script to the Editor directory when i tried to attach it to the ThirdPersonController i got a message say it’s editor script so it didn’t attach it.

So in the link i tried the other solution and did in the top of the script:

#if UNITY_EDITOR
// Editor specific code here
using UnityEditor;
#endif

And i’m not using [InitializeOnLoad] and i moved the script from the Editor directory back to the original directory of my scripts i have.

Then i attached the script to the ThirdPersonController and now when i make Build And Run i’m getting error:

error CS0246: The type or namespace name `EditorWindow’ could not be found. Are you missing a using directive or an assembly reference?

What is the EditorWindow ? Is that happen now since i put the using UnityEditor; inside the #if ?

Not sure what i s going on.
But i know that if i’m not attaching the script to something in the Hierarchy it will not run the Update function.

So it all messed up now.