Sign in
Log inSign up

Fighting Entropy In Unity – Warnings

gal bartouv
·Dec 26, 2021·

3 min read

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

TODO: show example of code that throws a warning here…

Warnings are there to warn you but not all warnings were created equal. Unity is trigger happy with warnings, but some times an empty serialized field is that way on purpose and some times you have unreachable code because you left a “todo” to work on later. You dont need to be reminded of this things every time you run your project. Whats even worse is that as your project grows and expands you will undoubtedly add plugins to your project, and i guaranty that these plugins will bring you a bundle of warnings free of charge.

Its really easy to reach a point where your project has tens if not hundreds of warnings on startup and scores more that popup up during its lifetime. When a warning that is of consequence appears you will miss it in the mountain of warnings you have piled up.

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 in the top of classes that you dont 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 its good you don’t have too.

CSC.RSP FILE With the csc file you can completely ignore specific warnings in your project. All you need to do is create 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 your 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.