Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Alternative to C# default parameters?

Discussion in 'Getting Started' started by MikeTeavee, Dec 18, 2015.

  1. MikeTeavee

    MikeTeavee

    Joined:
    May 22, 2015
    Posts:
    194
    I want to use default parameters on this "lightswitch" function. However, the Unity compiler doesn't allow default parameters; so I get a compiler error.

    (Note: code is simplified for this thread)
    Code (CSharp):
    1. // BAD CODE
    2. void SwitchUp(float AnimationSpeed = 1.0f) {
    3.         SwitchAnim ["MySwitchAnimation"].speed = AnimationSpeed;
    4.         SwitchAnim.Play ("MySwitchAnimation");
    5.     }
    I've read that you can change to another compiler, but I want my code shareable. So now, my function looks like this:

    Code (CSharp):
    1. // global variable
    2. public float AnimationSpeed;
    3.  
    4. void SwitchUp() {
    5.         SwitchAnim ["MySwitchAnimation"].speed = AnimationSpeed;
    6.         SwitchAnim.Play ("MySwitchAnimation");
    7.     }
    When I call this function passing a value of 3, I have to put:

    Code (CSharp):
    1.  
    2. float PrevAnimationSpeed = AnimationSpeed;
    3. AnimationSpeed = 3.0f;
    4. SwitchUp();
    5. AnimationSpeed = PrevAnimationSpeed;
    6.  
    Messy I know, and potentialy dangerous (if I ever get an error in SwitchUp())

    Don't ask why, but I plan to call this function several times with different values for AnimationSpeed. That's a lot of extra lines of code!

    Is there a better coding technique?
     
    Last edited: Dec 18, 2015
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    You can and I do use default parameters often.

    Your supposedly bad code compiles just fine.
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    What compiler error? I use default parameters all the time. They work fine.

    And if they didn't, yes, there would be a better work-around (method overloading). Do NOT use a global variable in place of a parameter, or you will suffer greatly.
     
  4. MikeTeavee

    MikeTeavee

    Joined:
    May 22, 2015
    Posts:
    194
    MyLightSwitchScript.cs(26,26): Error CS0241: Default parameter specifiers are not permitted (CS0241) (Assembly-CSharp)

    Just discovered I'm using .NET 3.5 framework which is the problem. The thing is, I don't want make it mandatory to change the compiler to 4.0 each time I compile this code on a different machine or share it online.

    Maybe I have no choice?
     
    Last edited: Dec 18, 2015
  5. MikeTeavee

    MikeTeavee

    Joined:
    May 22, 2015
    Posts:
    194
    ...just thought of this, perhaps I could use function overloading instead...
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Your only choices are .NET 2.0 or .NET 2.0 Subset. And default parameters work fine on both of those. I use these all over the place, in most of my projects I never change from the default .NET 2.0 Subset, and I have never seen this error.

    I suspect this is somehow coming from the editor you're using, rather than from Unity. Try closing MonoDevelop or VisualStudio or whatever. Edit your script (if needed) with a simple text editor, like Notepad on Windows or TextWrangler on the Mac. Save it and switch back to Unity. I bet it works.

    And then get Script Inspector 3, and wonder how you ever got by without it!
     
    Flipbookee and MikeTeavee like this.
  7. MikeTeavee

    MikeTeavee

    Joined:
    May 22, 2015
    Posts:
    194
    I'm confused about you saying that you can only use .NET2.0. Since I see many options under ProjectSettings>Editor.

    Also, I went to project settings in Unity and changed to .NET 4.0 Framework and I can now use default parameters... You think I should use .NET 2.0 instead?
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I'm talking about Project Setting > Player, "API Compatibility Level":


    I don't have anything like that under Project Settings > Editor:


    And yeah, I usually use .NET 2.0 Subset, unless I'm doing something that requires the full .NET 2.0 (like reflection). This is in Unity 5.3. Can you show me what you're talking about?
     
  9. MikeTeavee

    MikeTeavee

    Joined:
    May 22, 2015
    Posts:
    194
    ProjectSettings>Editor .... is incorrect sorry!!! ...I meant, in MonoDevelop, I was going to Project>Assembly-CSharpOptions>General.

    Here are all the .NET versions from 2.0 up to 4.6

    I just tried switching to different versions, but I still cannot get Default Parameters working.(ignore that I said 4.0 worked for me).
     
  10. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well there's your problem. I already suggested you quit using MonoDevelop and try a text editor (or better yet, Script Inspector 3) instead.

    What you're observing is a MonoDevelop problem, not a Unity problem. (Though it's still odd, because until recently I used MonoDevelop too, and I never had this issue.)
     
  11. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    No idea what you have broken.

    But for a dirty fix you can always do it old school. Before default parameters were a thing there was overloading.

    Code (CSharp):
    1. void SomeMethod () {
    2.     SomeMethod (false);
    3. }
    4.  
    5. void SomeMethod (bool something){
    6.     // do something
    7. }