Search Unity

Question Unity Build Errors

Discussion in 'Editor & General Support' started by HAMSOFT, Apr 1, 2023.

  1. HAMSOFT

    HAMSOFT

    Joined:
    May 31, 2020
    Posts:
    32
    Hello!
    I'm trying to compile my game for windows, but the console is showing up a lor of errors, this is the first time that I see them.
    The most common ones are: "The name 'PrefabUtility' does not exist in the current context", & "UnityEditor.BuildPlayerWindow+BuildMethodException: 2 errors at UnityEditor.BuildPlayerWindow+DefaultBuildMethods.BuildPlayer (UnityEditor.BuildPlayerOptions options) [0x002da] in <36f62d8e760b48f7af5d32916f997ce1>:0 at UnityEditor.BuildPlayerWindow.CallBuildMethods (System.Boolean askForBuildLocation, UnityEditor.BuildOptions defaultBuildOptions) [0x00080] in <36f62d8e760b48f7af5d32916f997ce1>:0".
    I have tried to solve it, but new ones appear:


    What Can I do to solve the issue?
     
  2. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    prefab utility is editor only and cannot be used in builds
     
  3. HAMSOFT

    HAMSOFT

    Joined:
    May 31, 2020
    Posts:
    32
    So I will need to look script by script that use it and rewrite it?
     
  4. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    you can use "find" in visual studio, select it to "entire project" and it will find all

    also you can add #if unity_editor

    so that the code is not included in the build
     
  5. HAMSOFT

    HAMSOFT

    Joined:
    May 31, 2020
    Posts:
    32
    I will need to add it to every single code that's giving the error
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    The error is reported in scripts that appear to use classes within the
    UnityEditor
    namespace.

    You may only use
    UnityEditor
    namespace methods and classes while you are in the Unity editor.

    You may never use anything from the
    UnityEditor
    namespace in a build.

    You can fix this in one of two ways:

    - wrap these bits of editor code in
    #if UNITY_EDITOR
    conditional compilation directives

    - you can put the entire script into a folder called
    Editor
    . This makes it what is loosely called an "editor script."

    Such "editor scripts" can never be attached to GameObjects or prefabs or scenes.
     
  7. HAMSOFT

    HAMSOFT

    Joined:
    May 31, 2020
    Posts:
    32
    The issue is that all of does scripts are default scripts made by Unity, I haven't made any of does scripts.
     
  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,840
    I think you've missed the point. It made be Unity code, but it is editor only code. Meaning you need to ensure they are not compiled into a final build.

    Find the scripts with this code, do as you've been instructed to do above. The errors point the scripts with this code.
     
  9. HAMSOFT

    HAMSOFT

    Joined:
    May 31, 2020
    Posts:
    32
    How may I find all of this scripts?
     
  10. HAMSOFT

    HAMSOFT

    Joined:
    May 31, 2020
    Posts:
    32
    Changing the Unity version may help?
     
  11. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,840
    Like I said, the errors tell you what scripts. Just click on them and work through each one.
     
  12. HAMSOFT

    HAMSOFT

    Joined:
    May 31, 2020
    Posts:
    32
    I have been going through each scrip that points the error and added the #if UNITY_EDITOR. I saw some messages on the console that make me wonder: Could the issue be related to the asset? upload_2023-4-2_0-17-44.png
     

    Attached Files:

  13. HAMSOFT

    HAMSOFT

    Joined:
    May 31, 2020
    Posts:
    32
    Well, this is awkward, after I saved the changes on the scripts and try to build again, the same erros will appear and guide me to the same scripts set to their default state, so the changes that I made are being undone.
     
  14. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,840
    Are you modifying package scripts? You should not be doing that.

    You need to fix the code in your scripts, if it wasn't abundantly clear so far.

    Something seems really wrong/broken with your project set up. What version of Unity are you on?
     
  15. HAMSOFT

    HAMSOFT

    Joined:
    May 31, 2020
    Posts:
    32
    I'm on the version 2021.3.4f1
     
  16. HAMSOFT

    HAMSOFT

    Joined:
    May 31, 2020
    Posts:
    32
    This is the first time I got this errors while building a game
     
  17. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,840
    It's probably the most common error you'll see when building a project. You just need to make sure all the editor-only code in your own scripts have been surrounded by preprocessor directives, or moved into Editor folders where applicable.

    That's all there is to it.
     
  18. HAMSOFT

    HAMSOFT

    Joined:
    May 31, 2020
    Posts:
    32
    The thing is that I barely use own scripts, all the code of the game is made with visual scripting graphs
     
  19. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    Do you remember ever using prefab utility at all? like ever?
     
  20. HAMSOFT

    HAMSOFT

    Joined:
    May 31, 2020
    Posts:
    32
  21. HAMSOFT

    HAMSOFT

    Joined:
    May 31, 2020
    Posts:
    32
    Changing the Unity version may help?
     
  22. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    It appears you have not read or understood a single reply here.

    Slow down and go back because every single person here has told you how to fix it.
     
  23. HAMSOFT

    HAMSOFT

    Joined:
    May 31, 2020
    Posts:
    32
    i have go through each code that was showing the erro adding the #if UNITY_EDITOR, also looking for them and seeing if they are on an Editor folder or adding them to one, I tried to move a folder that was on the library and said it was from the Assets, now is showing: "The name 'ReflectionMethodsCache' does not exist in the current context". It seems it will point an error with every single script, what can I do at this point?