Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

MenuItem is not working properly

Discussion in 'Scripting' started by qwertyuiop11110, Mar 25, 2020.

  1. qwertyuiop11110

    qwertyuiop11110

    Joined:
    Mar 16, 2020
    Posts:
    24
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.IO;
    4. using UnityEditor;
    5. using UnityEngine;
    6.  
    7.  
    8. [ExecuteInEditMode]
    9. public static class bugged
    10. {
    11.     [MenuItem("Come/On")]
    12.     public static void X()
    13.     {
    14.         string outputPath = Path.Combine(Application.dataPath, "Editor", "comeon");
    15.         BuildPipeline.BuildAssetBundles(outputPath, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
    16.     }
    17.  
    18. }
    Initially neither Visual Studio nor Unity throw any errors. Unity renders the button "Come" properly. When I click on "Come" then option "On", it appears to enter the processing but then following errors are thrown:
    Code (CSharp):
    1. Assets\bugged.cs(11,3): error CS0246: The type or namespace name 'MenuItemAttribute' could not be found (are you missing a using directive or an assembly reference?)
    Code (CSharp):
    1. Assets\bugged.cs(11,3): error CS0246: The type or namespace name 'MenuItem' could not be found (are you missing a using directive or an assembly reference?)
    Code (CSharp):
    1. Error building Player because scripts had compiler errors
    I cannot find any Unity namespace references in Visual Studio's "Add References" so I couldn't add any.
    Unity version is 2019.3.5f1, I have set .NET compatibility to 4.x. There's only one another script which is more advanced than this one. The code you see above is much more simplified version of the other.
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    It's because the script is using the UnityEditor namespace, which is only available in the editor, but it's trying to be included in a build. You need the script to be in an assembly that's not included in builds.

    The easiest fix is to put the script into a folder named "Editor", see here. An alternative is to use asmdef files to put the script into an assembly that's marked editor only.
     
  3. qwertyuiop11110

    qwertyuiop11110

    Joined:
    Mar 16, 2020
    Posts:
    24
    Fair enough, but I don't understand, why not throw a message regarding missing UnityEditor/UnityEngine? Why is this particular attribute causing issues compared to everything else.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    You're probably not using any UnityEditor stuff anywhere else in your code base.


    Unity should probably detect this pretty common error, and create a proper error message for it, rather than just letting the compiler fail in a normal way.
     
    qwertyuiop11110 likes this.
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    From an objective point of view: It's a compilation error, in particular one that was raised as it wasn't able to resolve a type or namespace. That's probably the reason. It has precedence over additional error analysis, especially since the latter could cause lots of additional overhead in huge code bases.

    That particular attribute belongs to the UnityEditor assembly, which is and must be available in the editor. As the editor assemblies are not included in the build, the type/namespace will not be resolvable as soon as you trigger the build. Hence you should let Unity know that this is only supposed to be an editor thing by moving it into an Editor folder, as recommended by @Baste.

    As stated, they could do additional analysis and tell the user about the editor dependency. Whether it's helpful or not... I don't know. Trying to explain and guide the user towards a proper fix for that problem might turn out to be difficult, especially when editor and application code is mixed. The only real benefit for those who already knew that error: they can fix it immediately. For those who have never had it and/or are new to this, you'd better link to a website that explains the entire thing.
     
    qwertyuiop11110 likes this.