Moving static class to another folder leads to errors

Hello all,

I want to build my game, to start profiling as a standalone game, but I am getting this error:
image

I found here an Q&A, that I need to move all scripts, which are using UnityEditor to the Editor folder. So I did.

image

But there is a problem with one particular script, where I have a lot on helper methods, therefore I will not paste here the whole script. The Script is static:

using Object = UnityEngine.Object;
using System.Collections;
using UnityEditor;
using UnityEngine;
using System.IO;
using System;

public static class HelperUtility
{
    public static void Meethod 1() {}
    public static void Meethod 2() {}
    public static void Meethod 3() {}
}

After I have moved this script to the Editor folder, I immediately got errors even before starting the game. For example:

  • Assets\Scripts\Player\PickupManager.cs(156,36): error CS0103: The name ‘HelperUtility’ does not exist in the current context
  • Assets\Scripts\ProcAnim\ProceduralAnimations.cs(113,42): error CS1061: ‘Transform’ does not contain a definition for ‘HelperUtility’ and no accessible extension method ‘HelperUtility’ accepting a first argument of type ‘Transform’ could be found (are you missing a using directive or an assembly reference?)
    Assets\Scripts\Quests\Quest2.cs(58,28): error CS0103: The name ‘HelperUtility’ does not exist in the current context

I don’t understand, why moving this script causes these errors? I guess there is something I don’t understand? How can use the script from the Editor folder?

Thank you
Huxi

Folders with the name “Editor” have a special treatment , everything in them gets stripped from the build since (as the name suggests) they are available only in the Editor , see this for more info.

If you have scripts that are only accessible in the editor ex : EditorWindows , custom drawers , AssetImporters , etc …) then it’s safe to put them in an Editor folder.

If you have scripts that are shared between editor and runtime , then put them in non-Editor folder since then they will be accessible by both.

And if you really need to have editor stuff inside runtime code (for example , do some Editor stuff in OnValidate) then you can use C# preprocessor directives.

using Object = UnityEngine.Object;
using System.Collections;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
using System.IO;
using System;

public static class HelperUtility
{
    public static void Meethod 1() 
{
      #if UNITY_EDITOR
           AssetDatabase.Refresh(); // example of clear use of "editor-only" classes
      #endif
}
    public static void Meethod 2() {}
    public static void Meethod 3() {}
}

in the example above , the code between #if UNITY_EDITOR and #endif will only be considered if you’re in the editor , else that chunk of code will pretty much be ignored during builds