Fighting Entropy In Unity – Warnings

Warnings

Warnings are emitted by the compiler. They indicate that there is a potential problem with your code. The difference between warnings and errors is that the compiler will still let you compile your code with warnings but won't if you have errors.

In a lot of projects, I worked on sooner rather than later the project has 600+ warnings on startup and more when running the game. A lot of these warnings are caused by third-party plugins, empty serialized fields, or other unharmful things.

The problem is that when a helpful warning pops up you won't see them. You'll miss them in a sea of useless warnings and thus warnings in the project lose all value.

So how do we resolve this issue?

PRAGMAS

A pragma is a language construct that tells the compiler how it should process input. Pragmas can be used to ignore warnings in specific classes. You can place them at the top of classes that you don't want to throw warnings. Below is an example of a pragma that ignores warning CS0414: The private field ‘X’ is assigned but its value is never used.

#pragma warning disable 0414

This is a good way to reduce the noise in your warning section of the console.

So the pragmas help but you’re probably thinking to yourself that there is no way you going to place warnings in hundreds of classes. So it's good you don’t have to.

CSC.RSP FILE

With the csc file, you can completely ignore specific warnings in your project. All you need to do is create a csc.rsp file and place it in your assets folder. That way you can reduce a lot of the warnings that are not helpful. At any time you can remove the ignored warnings from the csc file and check that you're not ignoring something important.

An example of the content of the file:

-nowarn:0649
-nowarn:0414 
-nowarn:0168

Adding the file above will ignore all of the following warnings, for all files in your project:

warning CS0649: Field ‘X’ is never assigned to, and will always have its default value null

warning CS0414: The private field ‘X’ is assigned but its value is never used

warning CS0168: variable declared but not used.