Question

I'm using Unity3D v4.2.2f1, and the version of MonoDevelop it comes bundled with - v2.8.2.

I see that there are cases where C# code will compile in Unity3D, but not in MonoDevelop. For instance, default values for method arguments are accepted by Unity3D, but result in "Default parameter settings are not permitted (CS0241)" in MonoDevelop.

General advice I've found online is to not attempt to compile from MonoDevelop. Fine - but I'm keen to understand how this mismatch arises. I thought that both Unity3D and MonoDevelop compile C# using Mono? Is it that Unity3D comes with two versions of Mono, one for itself and one for MonoDevelop? If so, why?

Example first line of method that compiles in Unity but not in MonoDevelop (the bit that doesn't compile in MonoDevelop but compiles from Unity3D is the '=false' assignment to setNative):

public static void SetUnityVersion(string path, bool setNative=false) {
    ...implementation...
}
Was it helpful?

Solution

When installed with Unity on Windows, MonoDevelop uses the Microsoft .Net Framework by default. And Unity uses "a custom build of Mono", as pointed out here.

The language feature that causes compiler error is called Optional parameters and was introduced in C# 4.0. Now, MonoDevelop projects generated by Unity target .Net Framework 3.5, which does not support this feature.

If you change target framework of generated projects to .Net 4.0 (in Monodevelop right-click on project, then options->general->Target Framework), everything should compile fine. But if you make any change to the project in Unity (add a script for example), MonoDevelop project files will be regenerated and your change will be lost.

To workaround this problem, you can download and install a release of Mono for Windows, then configure it as your default runtime in MonoDevelop (Tools->Options->.Net Runtimes->Add, find the installed Mono release, then hit Set as Default). Monodevelop will now use Mono to compile your code, and Mono compiler will not complain about default parameter values.

This should save you some trouble of switching between Unity and MonoDevelop just to check if the code compiles. Tested with Unity 4.5.3f3 and MonoDevelop 4.0.1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top