Search Unity

Compiler errors while compiling .exe

Discussion in 'Scripting' started by max00007, Jul 20, 2018.

  1. max00007

    max00007

    Joined:
    Feb 13, 2016
    Posts:
    19
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.UI;
    4. using UnityEditor;
    5. using UnityEngine;
    6.  
    7. public class FileManager : MonoBehaviour {
    8.     private string path;
    9.  
    10.     public void OpenExplorer()
    11.     {
    12.         path = EditorUtility.OpenFilePanel("Overwrite with png", "", "png");
    13.        
    14.     }
    15. }
    16.  
    The code above opens the windows file explorer. Well usually it does. There are no compiler erroes shown and the scene works when in testing it in Unity. It also works as long as the script is attached to a game object like a cube etc. The thing I´ve changed now is the fact that I´ve attached it to a canvas icon. Now each time I want to compile this for "Windows Stand Alone" I get the "Name "EditorUtility" does not exist in current context" error. Where is my mistake?

    Cheers & thanks in advance
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    You cannot use anything that's from UnityEditor in build. That is:
    Code (CSharp):
    1.  
    2. ...
    3. using UnityEditor;
    4.  
    5. ...
    6.         path = EditorUtility.OpenFilePanel("Overwrite with png", "", "png");
    7. ...
    8.  
    Also, you won't be able to compile the build, even if you simply include the "using UnityEdtior;" directive.

    Make sure to either wrap that in:
    Code (CSharp):
    1. #if UNITY_EDITOR
    2. using UnityEditor;
    3. #endif
    When you need it. Or remove it completely.