Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity Build error: The name 'EditorUtility' does not exist in the current context

Tags:

c#

unity3d

I have got a very simple problem. I have a build error when I am trying to build my game in unity. When I run the game in the editor it works just fine. Here is the error as it is shown in the console.

Assets\Scripts\Pattern.cs(284,17): error CS0103: The name 'EditorUtility' does not exist in the current context

Now, this is the line of code the error was referencing to:

EditorUtility.DisplayDialog("Great!", "You got the pattern right!", "Next Level!");

Lastly, if you think the error is that I didn't import the right things into the script then you are wrong because I imported this:

using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEditor;

can anybody help me? Thank you in advance.

EDIT: doing this code:

#if UNITY_EDITOR
            EditorUtility.DisplayDialog("Meh", "You got the pattern wrong ):", "Try Again!");
#endif

doesn't work. In the build version it just doesn't show the message box and it acts as though this line is just a comment. Can somebody help?

like image 786
INTODAN Avatar asked Jun 28 '19 22:06

INTODAN


2 Answers

The reason why you have errors now is, Unity remove "UnityEditor" namespace in compile time as they designed for it. That is why when you try to use it on platform, "EditorUtility" will never exist on any platform except UnityEditor. Because "EditorUtility" is in "UnityEditor" namespace.

so if you want do same jobs as you did in Unity editor using "EditorUtility" you should implement it your own as they do.

#if UNITY_EDITOR
    EditorUtility.DisplayDialog("Great!", "You got the pattern right!", "Next Level!");
#else
    YOUROWNCLASS.DisplayDialog("Great!", "You got the pattern right!", "Next Level!");
#endif
like image 94
Brian Choi Avatar answered Nov 14 '22 22:11

Brian Choi


UnityEditor content is typically only available for use in the Editor, as in, you can't use it when building your game as a standalone EXE.

Try adding preprocessor directives to only include the Editor-related stuff if you're actually compiling your game for the Editor.

#if UNITY_EDITOR
    EditorUtility.DisplayDialog("Great!", "You got the pattern right!", "Next Level!");
#endif

You also need to put the same #if UNITY_EDITOR ... #endif directives around your using UnityEditor; line, as @Retired Ninja informed us in the comments.

like image 34
nbura Avatar answered Nov 14 '22 21:11

nbura