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
10
Question by Matthew A · Jan 28, 2011 at 01:35 PM ·

c# optional parameters

I have a function like the following:

public static void Foo(Vector3 foo = Vector3.zero)
{
    //...
}

Which gives me an error: "The expression being assigned to optional parameter 'foo' must be constant or default value."

What am I doing wrong?

Comment
Add comment
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

4 Replies

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

Answer by justinl · Sep 21, 2012 at 11:55 AM

I believe the correct way to do it in C# is:

 public static void Foo(Vector3 bar = default(Vector3)){
 //this will default the Vector3 to 0,0,0
 }
Comment
Add comment · Show 7 · 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 SinisterRainbow · May 03, 2013 at 02:59 AM 0
Share

I believe this is the correct way as well, this should be thumbed up as the answer so others can benefit, it's the most elegant solution.

avatar image acatalept · Oct 28, 2013 at 05:46 PM 0
Share

This worked for me, thanks ;)

avatar image ankitmogo · Nov 19, 2013 at 01:01 AM 0
Share

can't upvote yet, but this answer worked for me. thanks!

avatar image Demigiant · Aug 28, 2014 at 08:44 AM 0
Share

Yup, correct way, but doesn't always work (for example in extension methods) because Unity does weird things with its internal structs :/

avatar image Minchuilla · Mar 28, 2020 at 08:04 PM 1
Share

You can leave out the type argument for the default as it can be implied. Foo(Vector3 bar = default) works

Show more comments
avatar image
8

Answer by Matthew A · Jan 28, 2011 at 02:32 PM

In the end I used a nullable type like so:

public static void Foo(Vector3? foo = null)
{
    Vector3 v = foo ?? Vector3.zero;
    // ...
}
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 rraallvv · Nov 20, 2016 at 01:40 PM 0
Share

Can this be used for the Color class like this?

 void DrawWire(Vector2 pr1, Vector2 pr2, Color color, Color? shadowColor = null)
avatar image
4

Answer by yoyo · Jan 28, 2011 at 05:06 PM

Another alternative is to use multiple entry points (method overloads) rather than optional parameters:

public static void Foo() { Foo(Vector3.zero); }

public static void Foo(Vector3 foo) { //... }

Also, for more information on optional parameters, see MSDN.

Comment
Add comment · 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
-3

Answer by zharramadar · Jan 28, 2011 at 01:54 PM

You are passing to the parameter the result of a property or attribute from another class, which C# cannot guarantee if it is really constant at compile-time. All optional parameters in C# must be constant at compile time.

As a workaround, you may do the following (saying out of my head, try it):

public static void Foo(Vector3 foo = new Vector3(0,0,0))
{
    //...
}

or just initialize it inside the class (as Vector3 is nullable), such as:

public static void Foo(Vector3 foo = null)
{
    if (foo == null) foo = Vector3.Zero;
    //...
}

There's also a OptionalParameter class in .NET, which I'm not sure if works under MONO.

There's a link that explains all that: http://msdn.microsoft.com/en-us/library/dd264739.aspx

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 Matthew A · Jan 28, 2011 at 02:27 PM 0
Share

Thanks for the answer, you put me on the right track, though new Vector3(0,0,0) didn't work either.

avatar image zharramadar · Jan 28, 2011 at 03:34 PM 0
Share

Yeah, I'm at work, so I couldn't test it in Unity. Glad it worked with the nullable thing.

avatar image originalNarf · Jan 25, 2014 at 01:53 AM 0
Share

The first method does not work me; it generates the same error as the original question.

avatar image Demigiant · Aug 28, 2014 at 08:42 AM 0
Share

Old answer, but still deserves to be noted that Vector3 is a struct and thus is NOT nullable, and the other option doesn't work because optional parameters need to be compile-time constants, so it's all wrong :P

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image


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