Categories
Unity3D-tips

#unity3d remove yellow warnings you don’t need #unitytips

Screen Shot 2016-04-16 at 11.48.08

Warnings are very, very important in any compiled language: they tell you that the computer has checked your code and realised you “probably” created a bug; they even tell you something about what the bug might be.

..but the computer isn’t sure – if it could be sure, it would be a compiler Error (in red). So (in Unity) it’s yellow, and “optional”. But in those cases where it’s not a bug – and you know it! – it’s very annoying. Most IDE’s let you turn them on and off, Unity doesn’t … here’s how to fix it.

NB: the global Disable technique can be used for a lot more, e.g. custom project-wide #defines – more info here: Unity Forum thread on SMCS etc

Check you really want to do this

If you know what you’re doing with compiler warnings, skip this section.

Everyone else: think carefully. A warning usually means one of the following:

  • You have a half-written class/algorithm you’re still working on, so there are bits of unused code, etc. NOT A BUG.
  • You’re using the programming language in an unusual way. MIGHT BE A BUG
    1. If the abuse is accidental: PROBABLY A BUG
    2. If the abuse is deliberate, expert-level: NOT A BUG
  • You typo’d a variable name, but accidentally wrote the name of a different variable. SERIOUS, MAJOR BUG

(especially note that last one: this is the worst kind of bug, one which you can look at 100 times and never see anything “wrong”, and which the computer will say is syntactically correct!)

So … even though many warnings are “wrong” (there’s no bug, and you know it), many of them are “right” and are your last line of defence against some of the nastiest bugs. Treat every warning on a case-by-case basis!

Option 1: Disable a warning EVERYWHERE

As an experienced C/C++/etc programmer, this is most useful to me. I write code like this:

[csharp]
public void MethodBlah( int param )
{
if( param < 1 )
; // PopupDialog( "Spammy in-game message I want when debugging this algorithm" );
else
{
// …
}
}
[/csharp]

In line 4, the shorthand “; //” lets you enable/disable a line of code with 4 keystrokes, using a built-in feature of C and all C-based languages. With modern IDE’s, there are usually better ways of doing this, but this serves as a small practical example.

This triggers a warning in Unity console:

Screen Shot 2016-04-16 at 12.00.28

In this case, we want to disable it EVERYWHERE; it’s a deliberate technique I’m using, so I don’t need (or want) the warning. I thought about consequences of the warning: it’s afraid you accidentally put a semi-colon on the same line as the “if” statement. It’s been almost 15 years since I last made that mistake; I’m confident I won’t make it again any time soon.

  1. Create a file “smcs.rsp”
  2. Save it in the Assets folder of your project
  3. Put one line per warning you want to suppress (see below)
  4. Modify any source file, and Unity will live reload and honour your new setting

Contents of smcs.rsp:

-nowarn: 1234

“1234” has to be the number that appeared in the Warning message in Unity console – in my case, at the end we have:

Screen Shot 2016-04-16 at 12.03.30

…so I put in my file:

-nowarn:0642

NB: this only works for game-scripts. To do the same for Editor scripts, you use a different file – gmcs.rsp

Option2: Disable a warning in ONE FILE only

For instance … you’re working on a new game-feature, and tweaking it. You have a bunch of variables you are tweaking, sometimes using them in the code, sometimes not. You get this annoying warning:

Screen Shot 2016-04-16 at 12.05.01

…for this ONE FILE ONLY, you want to ignore that particular warning. When you’ve finished your long tweaking sessions, you’ll re-instate that warning.

  1. Go to the very top of your file
  2. Add one #pragma line for each warning you want to disable (see below)

#pragma warning disable 1234

…again, the number is whatever number appeared in the Unity console after the letters “CS”. In my case: 0219

Option 3: Disable a warning for a SINGLE LINE OF CODE

(…or for a block of code)

This works the same as Option 2, except:

  1. Find the line(s) you want to ignore the warning(s) in
  2. Before the first line, add one #pragma warning disable for each warning
  3. After the last line, add one #pragma warning enable for each warning