optional function parameters in javascript and c#

how can i declare function parameters as optional? something like:

function foo(required:String, optional:float = 1.0) {
more ▼

asked Dec 04, 2009 at 12:44 PM

avatar image

806 75 65 101

Might be worth editing this question slightly so it's not Javascript specific, because the answer is applicable to both JS and C#

Dec 04, 2009 at 02:05 PM duck ♦♦

The comment above is not true anymore. As of C# 4.0, the optional parameters are supported (also in Unity since version 3.1, see @yoyo's answer below). They are still not available in UnityScript though (a.k.a. JS; tested today).

Jul 19, 2013 at 12:37 PM Simon Says


Question And Answers Outdated

since U3d version 4.0.

rest of page false or not complete answer.

Jun 07, 2014 at 11:00 PM frogsbo
(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

Unity's Javascript doesn't support optional parameters directly (and neither does C#), but it does support function overloading, so the correct way to achieve this is to define the function twice, once with both parameters, and once with just one parameter. You can then optionally have the single-parameter implementation just pass the default value through to the two-parameter implementation, like this:

function foo(str : String) { // pass 1.0 as the default value for 'num' foo(str, 1.0); }

function foo(str : String, num : float) { Debug.Log(str + " : " + num); }

And it might be worth adding that this is equally applicable when working in C# too, where it would look very similar:

private void foo(string str) { // pass 1.0 as the default value for 'num' foo(str, 1.0f); }

private void foo(string str, float num) { Debug.Log(str + " : " + num); }

more ▼

answered Dec 04, 2009 at 01:50 PM

avatar image

duck ♦♦
51.2k 147 408 545

duck, you rock! thanks!

Dec 04, 2009 at 03:19 PM flexrails
(comments are locked)
10|3000 characters needed characters left

As of Unity 3.1, default arguments are supported in C#. See MSDN for documentation on how they work. Note that Mono Develop doesn't like them, so if you're using the debugger you need to select Tools > Preferences > Unity > Debugger and turn off "Build project in MonoDevelop".

more ▼

answered Feb 04, 2011 at 09:21 PM

avatar image

9.1k 62 191 167

I can't find any evidence of support in JavaScript, and it's not in the 3.1 release notes.

Feb 13, 2011 at 12:45 AM Waz

I meant for C# (which I've tested). Tweaked my answer to be more explicit.

Feb 14, 2011 at 08:31 PM yoyo

Doesn't seem to work for me. I get errors for those function calls that don't have the 'optional' argument included in the call. Eg. foo("help"); errors with BCE0126: It is not possible to evaluate an expression of type 'void'. ? EDIT- this was because I had a yield statement within my function.

Oct 05, 2011 at 01:39 PM grimmy

This helped awesomely! Didn't know about the "Build project in MonoDevelop" thing, I always set the build for the project to C# 4.0, though that resets sometimes and really sucks. This seems permanent :D Thanks!

Jan 15, 2013 at 04:37 PM The Oddler
(comments are locked)
10|3000 characters needed characters left

Here's another way, in C# only as far as I know.

public void foo(required:String, params Object[] optionalArgs) {

more ▼

answered Mar 07, 2011 at 04:04 PM

avatar image

Bart Wttewaall
27 3 2 5

required:String is UnityScript ;) In C# we just do String required

Oct 11, 2011 at 01:40 PM Tseng

or even s`tring required` ;-)

Jul 19, 2013 at 12:27 PM Simon Says
(comments are locked)
10|3000 characters needed characters left

Here is a good explanation: http://www.tipstrs.com/tip/354/Using-optional-parameters-in-Javascript-functions. I hope it works in Unity as well!

more ▼

answered Dec 04, 2009 at 01:25 PM

avatar image

75 2 4

This won't work in Unity. And in general, you shouldn't rely on non-Unity-specific Javascript documentation (i.e. browser-based Javascript) as a guide to Unity's Javascript functionality. Unity's Javascript is not real Javascript, it only looks like it! This is because it is built on top of Mono, which is an open-source implementation of Microsoft's .Net platform. So, while it looks like Javascript, its functionality is defined by how Mono operates.

Dec 04, 2009 at 02:09 PM duck ♦♦

Hmm, not sure I fully agree. For C#, I use MSDN all the time for additional documentation that's not available from the Unity web site. Sure, you need to understand that not everything may apply, but it's still a useful resource.

Feb 04, 2011 at 09:22 PM yoyo

C# is a language, .NET and Mono are Frameworks. And Mono is the platform independent (and open source) version of .NET, so year: C# is equal in both Mono and .NET. But the .NET and Mono may be different or implement certain features differently. Maybe one of the Mono classes is implemented wrong, so you may get different results from .NET.

This has nothing to do with C#.

UnityScript on the other side is a completely separate programming language which has many similarities to JavaScript, but isn't JavaScript. Duck is right, while you can use all of C# features. However, some of the latest stuff doesn't work in C# & Mono (i.e. the 4.0 syntax functionality or the new async/await feature for threading), because Mono lags behind the original .NET Framework.

Oct 11, 2011 at 01:39 PM Tseng

Actually Mono isn't lagged much behind .NET at all.

But Unity uses a custom build of Mono that lags behind the open source released version by a good bit.

Nov 13, 2013 at 11:12 PM Jeff Kesselman
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Dec 04, 2009 at 12:44 PM

Seen: 27637 times

Last Updated: Jun 07, 2014 at 11:00 PM