How to force warnings as errors?

I’ve always considered it a best practive to crank the warning level of the compiler as high as it will go and to set warnings to be errors (so they break the build). This is a really good way to ensure that your team always writes ‘solid’ code because often warnings have valid risks associated with them.

Is it possible set warnings to be flagged as errors in Unity? And is it also possible to crank up warning level?

Thanks!

1 Like

Create a text file in ProjectFolder/Assets/smcs.rsp

Everything in that file will end up as compiler command-line parameters

The parameter to force warnings as errors is -warnaserror+, so add this to the smcs.rsp

If you are interested in other available parameters, run UnityInstallFolder/editor/data/bin/smcs /?

You should use mcs.rsp to have needed results for Unity 5.5+.

In ProjectSettings.asset you can add section (also possible via UI):

  additionalCompilerArguments:
    Standalone:
    - -warnaserror+

Works in 2021.3.

The way to turn on warnings as errors in 2022 is in the Additional Compiler Arguments in the Player Settings. Add -warnaserror+ there.

When using .NET 2.0 subset, -warnaserror+ will produce the following internal compiler errror:

Internal compiler error. See the console log for more information. output was:error CS0618: System.Security.Permissions.SecurityPermissionAttribute' is obsolete: CAS support is not available with Silverlight applications.’

It seams a Unity internal problem. Any one knows how to fix it?

The accepted answer here did not work for me. I am using unity 2019.4.5f1. I can’t seem to find any way to make this happen.

This post is very old, is there an updated way to do this for unity 2021+ ?

Adding the file and contents above just makes the warnings all disappear.

With .Net 4.x the compiler changed to CSC so the file needs to be called csc.rsp. Also see this manual page.