Incorrect Script Warnings in Editor

I’m getting several warning messages in the editor that appear to be incorrect. This has been especially annoying recently, as they’re making the actual error messages harder to see. Does anyone have any idea why the editor is giving these messages and how to stop it?

WARNING: Unreachable code detected

function GetNumUnlocked(type : String) : int
{
	switch(type)	// Points to this line
	{
		case "symbols":
			return mUnlockedSymbols[mCurRank];
		case "tiles":
			return mUnlockedTiles[mCurRank];
		case "bg1":
			return mUnlockedBG1[mCurRank];
		case "bg2":
			return mUnlockedBG2[mCurRank];
 	}
	
	return 1;
}

I’ve already tried adding a default case and adding redundant break statements after the returns, to no effect.

WARNING: Implicit downcast from ‘Object’ to ‘UnityEngine.Transform’

for(var child : Transform in tScroll)
{
	...
}

I’ve tried adding ‘as Transform’ after tScroll, to no effect.

For the second one, use #pragma downcast. For the first, there’s not much you can do except to rewrite the logic a bit so as not to have returns in the switch statement.

–Eric

Ninjaaaaa’d.

Best not to return from a switch, anyway. Logic gets hard to follow when that happens.

Thanks, that fixed it. Bit of an odd quirk there, but at least its solvable.

the unreachable code is not the line: return 1; Because all other branches return something, that line will never be called.(Edit: I was wrong on this)

As an aside, and not really your problem, having to keep parallel arrays is generally bad form. Instead define a struct or class such as NumberOfUnlocks or whatever the class represents and define its members and store instances of that class in an array.

class NumberOfUnlocks {
   public var symbols:int;
   public var tiles:int;
   public var bg1:int;
   public var b2:int;

//not necessary but if you want a string accessor (another bad idea)
   public function GetNumUnlocked (type:String) :int
   {
      var retVal:int = 1; // your default return in case the type is not recognized
      if ( "symbols"==type) {
         retVal = symbols;
      } else if ("tiles"=type) {
              retVal = tiles;
      } else if ("bg1" = type) {
             retVal = bg1;
      } else if ( "bg2" = type ) {
              retVal = bg2;
      }
      return retVal;
   }

Then wherever you were using the other function do this instead:
var symbolsUnlocked = mUnlocked[mCurRank].GetNumUnlocked ("symbols"); or just grab it directly like:

var symbolsUnlocked = mUnlocked[mCurRank].symbols;

Return 1 is reachable since there’s no default case in the switch statement. The compiler is actually whining because once you get into the first case, you unconditionally return, but if the line was not ‘return’ execution would continue down the case (to the next return, since there’s no breaks).

Also don’t forget breaks in your switch :stuck_out_tongue: That’s another nasty bug to track down.

you are correct, I assume the breaks.

Edit: After further investigation, the returns should not cause unreachable code.

It seems that unityscript has a problem and it’s recommended to use if-else-if blocks.

having returns in C# does not cause the problems. What would cause problems is if there were a break; after a return in a case. Then that break is unreachable.

I edited the code above.

old forum post, in which the last poster says that it still existed in 3.4 even when using returns without breaks

Actually the Unityscript compiler for switch/case is simply not programmed correctly when it comes to the “unreachable code” warning if you’re using return inside the switch. This code:

var x = 0;
switch (x) {
	case 0:
		Debug.Log ("0");
		return;
	case 1:
		Debug.Log ("1");
		return;
}
Debug.Log ("X");

will print the warning in Unityscript, but not C#. It may not be the best idea to use this sort of structure, but the warning is just wrong. (On the other hand, Unityscript switch/case supports C+±style fall-through, which C# forbids, so there’s that.)

–Eric