{"id":3845,"date":"2016-04-16T12:18:20","date_gmt":"2016-04-16T11:18:20","guid":{"rendered":"http:\/\/t-machine.org\/?p=3845"},"modified":"2016-04-16T12:18:47","modified_gmt":"2016-04-16T11:18:47","slug":"unity3d-remove-yellow-warnings-you-dont-need-unitytips","status":"publish","type":"post","link":"http:\/\/new.t-machine.org\/index.php\/2016\/04\/16\/unity3d-remove-yellow-warnings-you-dont-need-unitytips\/","title":{"rendered":"#unity3d remove yellow warnings you don&#8217;t need #unitytips"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/t-machine.org\/wp-content\/uploads\/Screen-Shot-2016-04-16-at-11.48.08.png\" alt=\"Screen Shot 2016-04-16 at 11.48.08\" width=\"766\" height=\"130\" class=\"aligncenter size-full wp-image-3846\" srcset=\"https:\/\/t-machine.org\/wp-content\/uploads\/Screen-Shot-2016-04-16-at-11.48.08.png 766w, https:\/\/t-machine.org\/wp-content\/uploads\/Screen-Shot-2016-04-16-at-11.48.08-150x25.png 150w, https:\/\/t-machine.org\/wp-content\/uploads\/Screen-Shot-2016-04-16-at-11.48.08-300x51.png 300w, https:\/\/t-machine.org\/wp-content\/uploads\/Screen-Shot-2016-04-16-at-11.48.08-624x106.png 624w\" sizes=\"(max-width: 766px) 100vw, 766px\" \/><\/p>\n<p>Warnings are very, very important in any compiled language: they tell you that the computer has checked your code and realised you &#8220;probably&#8221; created a bug; they even tell you something about what the bug might be.<\/p>\n<p>..but the computer isn&#8217;t sure &#8211; if it could be sure, it would be a compiler Error (in red). So (in Unity) it&#8217;s yellow, and &#8220;optional&#8221;. But in those cases where it&#8217;s not a bug &#8211; and you know it! &#8211; it&#8217;s very annoying. Most IDE&#8217;s let you turn them on and off, Unity doesn&#8217;t &#8230; here&#8217;s how to fix it.<br \/>\n<!--more--><\/p>\n<p>NB: the global Disable technique can be used for a lot more, e.g. custom project-wide #defines &#8211; more info here: <a href=\"http:\/\/forum.unity3d.com\/threads\/how-to-set-project-wide-pragma-directives-with-javascript.71445\/\">Unity Forum thread on SMCS etc<\/a><\/p>\n<h2>Check you really want to do this<\/h2>\n<p>If you know what you&#8217;re doing with compiler warnings, skip this section.<\/p>\n<p>Everyone else: think carefully. A warning usually means one of the following:<\/p>\n<ul>\n<li>You have a half-written class\/algorithm you&#8217;re still working on, so there are bits of unused code, etc. NOT A BUG.\n<li>You&#8217;re using the programming language in an unusual way. MIGHT BE A BUG\n<ol>\n<li>If the abuse is accidental: PROBABLY A BUG\n<li>If the abuse is deliberate, expert-level: NOT A BUG\n<\/ol>\n<li>You typo&#8217;d a variable name, but accidentally wrote the name of a different variable. SERIOUS, MAJOR BUG\n<\/ul>\n<p>(especially note that last one: this is the worst kind of bug, one which you can look at 100 times and never see anything &#8220;wrong&#8221;, and which the computer will say is syntactically correct!)<\/p>\n<p>So &#8230; even though many warnings are &#8220;wrong&#8221; (there&#8217;s no bug, and you know it), many of them are &#8220;right&#8221; and are your last line of defence against some of the nastiest bugs. Treat every warning on a case-by-case basis!<\/p>\n<h2>Option 1: Disable a warning EVERYWHERE<\/h2>\n<p>As an experienced C\/C++\/etc programmer, this is most useful to me. I write code like this:<\/p>\n<p>[csharp]<br \/>\npublic void MethodBlah( int param )<br \/>\n{<br \/>\n    if( param &lt; 1 )<br \/>\n        ; \/\/ PopupDialog( &quot;Spammy in-game message I want when debugging this algorithm&quot; );<br \/>\n    else<br \/>\n    {<br \/>\n       \/\/ &#8230;<br \/>\n    }<br \/>\n}<br \/>\n[\/csharp]<\/p>\n<p>In line 4, the shorthand &#8220;; \/\/&#8221; 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&#8217;s, there are usually better ways of doing this, but this serves as a small practical example.<\/p>\n<p>This triggers a warning in Unity console:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/t-machine.org\/wp-content\/uploads\/Screen-Shot-2016-04-16-at-12.00.28.png\" alt=\"Screen Shot 2016-04-16 at 12.00.28\" width=\"763\" height=\"34\" class=\"aligncenter size-full wp-image-3847\" srcset=\"https:\/\/t-machine.org\/wp-content\/uploads\/Screen-Shot-2016-04-16-at-12.00.28.png 763w, https:\/\/t-machine.org\/wp-content\/uploads\/Screen-Shot-2016-04-16-at-12.00.28-150x7.png 150w, https:\/\/t-machine.org\/wp-content\/uploads\/Screen-Shot-2016-04-16-at-12.00.28-300x13.png 300w, https:\/\/t-machine.org\/wp-content\/uploads\/Screen-Shot-2016-04-16-at-12.00.28-624x28.png 624w\" sizes=\"(max-width: 763px) 100vw, 763px\" \/><\/p>\n<p>In this case, we want to disable it EVERYWHERE; it&#8217;s a deliberate technique I&#8217;m using, so I don&#8217;t need (or want) the warning. I thought about consequences of the warning: it&#8217;s afraid you accidentally put a semi-colon on the same line as the &#8220;if&#8221; statement. It&#8217;s been almost 15 years since I last made that mistake; I&#8217;m confident I won&#8217;t make it again any time soon.<\/p>\n<ol>\n<li>Create a file &#8220;smcs.rsp&#8221;\n<li>Save it in the Assets folder of your project\n<li>Put one line per warning you want to suppress (see below)\n<li>Modify any source file, and Unity will live reload and honour your new setting\n<\/ol>\n<p>Contents of smcs.rsp:<\/p>\n<blockquote><p>\n-nowarn: 1234\n<\/p><\/blockquote>\n<p>&#8220;1234&#8221; has to be the number that appeared in the Warning message in Unity console &#8211; in my case, at the end we have:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/t-machine.org\/wp-content\/uploads\/Screen-Shot-2016-04-16-at-12.03.30.png\" alt=\"Screen Shot 2016-04-16 at 12.03.30\" width=\"139\" height=\"35\" class=\"aligncenter size-full wp-image-3848\" \/><\/p>\n<p>&#8230;so I put in my file:<\/p>\n<blockquote><p>\n-nowarn:0642\n<\/p><\/blockquote>\n<p><em>NB: this only works for game-scripts. To do the same for Editor scripts, you use a different file &#8211; gmcs.rsp<\/em><\/p>\n<h2>Option2: Disable a warning in ONE FILE only<\/h2>\n<p>For instance &#8230; you&#8217;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:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/t-machine.org\/wp-content\/uploads\/Screen-Shot-2016-04-16-at-12.05.01.png\" alt=\"Screen Shot 2016-04-16 at 12.05.01\" width=\"906\" height=\"30\" class=\"aligncenter size-full wp-image-3849\" srcset=\"https:\/\/t-machine.org\/wp-content\/uploads\/Screen-Shot-2016-04-16-at-12.05.01.png 906w, https:\/\/t-machine.org\/wp-content\/uploads\/Screen-Shot-2016-04-16-at-12.05.01-150x5.png 150w, https:\/\/t-machine.org\/wp-content\/uploads\/Screen-Shot-2016-04-16-at-12.05.01-300x10.png 300w, https:\/\/t-machine.org\/wp-content\/uploads\/Screen-Shot-2016-04-16-at-12.05.01-768x25.png 768w, https:\/\/t-machine.org\/wp-content\/uploads\/Screen-Shot-2016-04-16-at-12.05.01-624x21.png 624w\" sizes=\"(max-width: 906px) 100vw, 906px\" \/><\/p>\n<p>&#8230;for this ONE FILE ONLY, you want to ignore that particular warning. When you&#8217;ve finished your long tweaking sessions, you&#8217;ll re-instate that warning.<\/p>\n<ol>\n<li>Go to the very top of your file\n<li>Add one #pragma line for each warning you want to disable (see below)\n<\/ol>\n<blockquote><p>\n#pragma warning disable 1234\n<\/p><\/blockquote>\n<p>&#8230;again, the number is whatever number appeared in the Unity console after the letters &#8220;CS&#8221;. In my case: 0219<\/p>\n<h2>Option 3: Disable a warning for a SINGLE LINE OF CODE<\/h2>\n<p>(&#8230;or for a block of code)<\/p>\n<p>This works the same as Option 2, except:<\/p>\n<ol>\n<li>Find the line(s) you want to ignore the warning(s) in\n<li>Before the first line, add one #pragma warning <strong>dis<\/strong>able for each warning\n<li>After the last line, add one #pragma warning <strong>en<\/strong>able for each warning\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Warnings are very, very important in any compiled language: they tell you that the computer has checked your code and realised you &#8220;probably&#8221; created a bug; they even tell you something about what the bug might be. ..but the computer isn&#8217;t sure &#8211; if it could be sure, it would be a compiler Error (in [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69],"tags":[],"_links":{"self":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts\/3845"}],"collection":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/comments?post=3845"}],"version-history":[{"count":3,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts\/3845\/revisions"}],"predecessor-version":[{"id":3852,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts\/3845\/revisions\/3852"}],"wp:attachment":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/media?parent=3845"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/categories?post=3845"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/tags?post=3845"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}