← Back to Articles

Warnings

Article banner: Fighting Entropy In Unity, Warnings

Compiler warnings are messages that alert you to potential issues in your code. While they don’t prevent your code from being compiled, it’s important to address warnings as they could indicate problems that could cause issues when running your program. On the other hand, errors are critical issues that must be fixed before the code can be compiled.

I have often encountered projects that have a large number of warnings at the start, sometimes over 600, and sometimes even more when the program is running. These warnings can be caused by a variety of factors, such as third-party plugins, empty serialized fields, or other benign issues. It is important to address these warnings as soon as possible in the project to ensure the smooth operation of the program.

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.