2
\$\begingroup\$

Accordingly to this thread, it seems that default parameters aren't supported by C# script in Unity3D environment.

Declaring an optional parameter in a C# scirpt makes Mono Ide complaint about it:

void Foo(int first, int second = 10) // this line is marked as wrong inside Mono IDE

Anyway if I ignore the error message of Mono and run the script in Unity, it works without notify any error inside Unity Console.

Could anyone clarify a little bit this issue? Particularly:

  • Are default parameters allowed inside C# scripts?
  • If yes, are they supported by all platforms?
  • Why Mono complains about them if the actually works?
\$\endgroup\$
1
  • \$\begingroup\$ In fact you can build in Visual Studio targeting .NET 3.5 with no problems. \$\endgroup\$
    – felipe
    Oct 10, 2013 at 15:34

1 Answer 1

2
\$\begingroup\$
  1. As of Unity 3.1, default arguments are supported in C#. As Mono Develop doesn't support them, so if you're using the debugger you need to select Tools > Preferences > Unity > Debugger and turn off "Build project in MonoDevelop".

  2. Yes supported.

  3. As before C# 4.0, doesn't allow for default parameters. For this reason it was not at Unity3D. Mono complains why, Mono can answer the best. But I think they will add at next version.

\$\endgroup\$
1
  • \$\begingroup\$ I just find out that MonoBehaviors with default parameters aren't allowed if the class is declared inside a namespace. Don't know if it is a bug. \$\endgroup\$
    – Heisenbug
    Oct 10, 2013 at 16:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .