Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
8
Question by flexrails · Dec 04, 2009 at 12:44 PM · syntax

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) {
...
}
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image duck ♦♦ · Dec 04, 2009 at 02:05 PM 0
Share

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

avatar image Simon Says · Jul 19, 2013 at 12:37 PM 0
Share

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).

avatar image frogsbo · Jun 07, 2014 at 11:00 PM 0
Share

-----=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=-

Question And Answers Outdated

since U3d version 4.0.

rest of page false or not complete answer.

4 Replies

· Add your reply
  • Sort: 
avatar image
18
Best Answer

Answer by duck · Dec 04, 2009 at 01:50 PM

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); }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image flexrails · Dec 04, 2009 at 03:19 PM 0
Share

duck, you rock! thanks!

avatar image
7

Answer by yoyo · Feb 04, 2011 at 09:21 PM

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".

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Waz · Feb 13, 2011 at 12:45 AM 0
Share

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

avatar image yoyo · Feb 14, 2011 at 08:31 PM 1
Share

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

avatar image grimmy · Oct 05, 2011 at 01:39 PM 0
Share

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.

avatar image The-Oddler · Jan 15, 2013 at 04:37 PM 0
Share

This helped awesomely! Didn't know about the "Build project in $$anonymous$$onoDevelop" 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!

avatar image
1

Answer by Bart Wttewaall · Mar 07, 2011 at 04:04 PM

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

public void foo(required:String, params Object[] optionalArgs) {
    Debug.Log(optionalArgs.Length);
}

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tseng · Oct 11, 2011 at 01:40 PM 1
Share

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

avatar image Simon Says · Jul 19, 2013 at 12:27 PM 0
Share

or even s`tring required` ;-)

avatar image
-1

Answer by elevishstar · Dec 04, 2009 at 01:25 PM

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!

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image duck ♦♦ · Dec 04, 2009 at 02:09 PM 2
Share

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 $$anonymous$$ono, which is an open-source implementation of $$anonymous$$icrosoft's .Net platform. So, while it looks like Javascript, its functionality is defined by how $$anonymous$$ono operates.

avatar image yoyo · Feb 04, 2011 at 09:22 PM 0
Share

Hmm, not sure I fully agree. For C#, I use $$anonymous$$SDN 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.

avatar image Tseng · Oct 11, 2011 at 01:39 PM 1
Share

C# is a language, .NET and $$anonymous$$ono are Frameworks. And $$anonymous$$ono is the platform independent (and open source) version of .NET, so year: C# is equal in both $$anonymous$$ono and .NET. But the .NET and $$anonymous$$ono may be different or implement certain features differently. $$anonymous$$aybe one of the $$anonymous$$ono 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 program$$anonymous$$g 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# & $$anonymous$$ono (i.e. the 4.0 syntax functionality or the new async/await feature for threading), because $$anonymous$$ono lags behind the original .NET Framework.

avatar image Jeff-Kesselman · Nov 13, 2013 at 11:12 PM 0
Share

Actually $$anonymous$$ono isn't lagged much behind .NET at all.

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Using var as a placeholder for velocity... why? 2 Answers

how to typecast from GameObject 1 Answer

UnityScript, JavaScript, C# 1 Answer

Item Scripting Error 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges