Unity Build Errors

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?

prefab utility is editor only and cannot be used in builds

So I will need to look script by script that use it and rewrite it?

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

I will need to add it to every single code that's giving the error

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.

The issue is that all of does scripts are default scripts made by Unity, I haven't made any of does scripts.

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.

How may I find all of this scripts?

Changing the Unity version may help?

Like I said, the errors tell you what scripts. Just click on them and work through each one.

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? 8921300--1222118--upload_2023-4-2_0-17-44.png


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.

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?

I'm on the version 2021.3.4f1

This is the first time I got this errors while building a game

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.

The thing is that I barely use own scripts, all the code of the game is made with visual scripting graphs

Do you remember ever using prefab utility at all? like ever?