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. Dismiss Notice

How to cause build to fail

Discussion in 'Editor & General Support' started by uglybeardgames, Jan 16, 2019.

  1. uglybeardgames

    uglybeardgames

    Joined:
    Jul 28, 2018
    Posts:
    6
    I am trying to cause the build to fail with an editor script when it detects a prefab in the scene. I have the prefab detection working fine but I am unable to figure out how to actually get the build to fail.

    Debug.LogError() gets an error in the console output, build still successful.

    Throwing a BuildFailedException gets an error in the console output, build still successful.

    I want the build to fail and stop right when my editor script detects the prefab. I'm using IProcessScene and OnProcessScene() as the method of prefab detection.

    How do you get the build to fail?
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,915
  3. uglybeardgames

    uglybeardgames

    Joined:
    Jul 28, 2018
    Posts:
    6
    Build still continues and completes reporting "succeeded".
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,580
    I just wonder, since you know you got error, why you are building project? It is manual process anyway. Or you have some automation in place?
     
  5. uglybeardgames

    uglybeardgames

    Joined:
    Jul 28, 2018
    Posts:
    6
    I am creating an editor script that detects a specific condition and will cause the build to fail as a result.
     
    Antypodish likes this.
  6. ycanatilgan

    ycanatilgan

    Joined:
    Aug 23, 2017
    Posts:
    30
    Bump. I found a way to do it but there's a problem. The error appears not just while building but compiling.

    I used OnValidate() function:

    Code (CSharp):
    1. bool debug;
    2.  
    3. private void OnValidate()
    4.     {
    5.         if (debug)
    6.         {
    7.             Debug.LogError("Build failed because one of your CharacterSpawner is in debug mode! " +
    8.                 "Scene: " + SceneManager.GetActiveScene().name + " GameObject: " + gameObject.name);
    9.         }
    10.     }
    It forces build to fail but the error occurs at other times as well. How can I make it only happen while building?