Internal compiler error. (...) warning CS0162: Unreachable code detected

Most of times I get “Internal compiler error”, I just edit anything in any file so it will recompile and then it goes away. Restarting Unity is another way of doing that same thing: triggering the compiler.

This time nothing of that worked.

What could have happened?

SisterKy have already compiled a fine list for the “what could have happened” part.

The warning, at other hand, gives a very specific information about what did happen: “Unreachable code”. I think it is still a compiler bug because of this simple test which does reproduce the same error (which was coincidentally already mentioned by cowlinator elsewhere). Just create an empty project and add this file:

using UnityEngine;
using System.Collections;

public class test : MonoBehaviour
{
	const // simply remove this line, and error will disappear
		bool foo = false;
	
	void Start ()
	{
		StartCoroutine( bar() );
	}
	
	IEnumerator bar ()
	{
		yield return 0;
		
		if (foo)
		{
			yield return 0;
		}
		
		Debug.Log("finished!");
	}
}

Once you know where this little bug is located it becomes pretty simple to find many ways to go around it. One way is using static readonly instead of const.

changing something from const to static will certainly get rid of this warning, but it also changes the semantics of your code.

imo a better way to do this is to bracket the offending code between

#pragma warning disable 0429 and #pragma warning restore 0429.

(0429 is what worked for me, but you might need to use 0162).