I want to use a pre-build script for incrementing a build number, but the script is not even executed when running a build and I don’t know why. Neither the warning nor the exception is shown. Build runs without errors. This is the class:
using System;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
namespace My.Editor.Scripts.Project
{
public class BuildIncrementor : IPreprocessBuildWithReport
{
/// <inheritdoc />
public int callbackOrder => 1;
/// <inheritdoc />
public void OnPreprocessBuild(BuildReport report)
{
Debug.LogWarning("BUILD-INCREMENTOR");
throw new Exception("BUILD-INCREMENTOR");
}
}
}
The class is located in a separate assembly using the following assembly definition:
Any ideas how to get the pre-build script executed?
That might be part of the problem. Put it in the usual default assembly. Does it work?
If not, here’s one that does:
https://discussions.unity.com/t/824990
Thanks for your anwer. I exactly tried to build something upon this video tutorial.
I found out that the code in the class implementing IPreprocessBuildWithReport was executed all the time. I tested with and without assembly definition and it works in both cases at least in Unity 2022.3. But there are two special things happening which made me believe that the code was not executed:
- Logs and errors within the pre-build class are cleared in the console by default, so you can’t see them there anymore after the build has been finished.
- The build is not aborted when an error happens inside the pre-build class so the build will still be finished as if nothing special had happened.
The most important first thing can be prevented with turning off the “Clear on Recompile” flag on the console in the Unity Editor:

The second thing is only good to know.