Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Why can't I make void stuff(Color line = Color.cyan)

Discussion in 'Scripting' started by XukeLho, Mar 28, 2016.

Thread Status:
Not open for further replies.
  1. XukeLho

    XukeLho

    Joined:
    Dec 30, 2014
    Posts:
    199
    Well, the title prety much says it all.

    I want this but it doesn't work
    Code (CSharp):
    1. void stuff(Color line = Color.cyan)
    This works but its not what I want
    Code (CSharp):
    1. void stuff(Color line)
    I want to create a function that receives a color as parameter, but not as a mandatory one, so I'm giving it a default value. But can't do that because 'line' must be a compile-time constant, whatever that means.
    Options?
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    You could try creating a private variable specifically to store Color.cyan, and apply the variable as the default value for the function parameter. No promises though, just a thought experiment.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    this is where function overloading comes in.

    Write one function with the parameter, write another with the parameter. When the functions are used the compiler which match which function should be used based on it's "shape" (i.e. the types of the parameters).
     
    p_lewis and XukeLho like this.
  4. XukeLho

    XukeLho

    Joined:
    Dec 30, 2014
    Posts:
    199
    Ended up doing the overload as LeftyRighty sugested. Still don't know why that happens though.
     
  5. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,381
  6. XukeLho

    XukeLho

    Joined:
    Dec 30, 2014
    Posts:
    199
  7. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    'Let me lock this thread because XukeLho can't be bothered to learn'.
     
    Graysome, Kasaie, reggie5538 and 2 others like this.
Thread Status:
Not open for further replies.