- Home /
U3D version 4 optional function parameters in javascript and c#
Can someone please demonstrate the Unity3d optional function parameters? i.e.
function human(name:string,required; age: int, optional; shoesize:float,optional; haircolor : Color : optional)
...
is there a way of invoking the above function with different combinations, just say the human'name and hair color, the human's name and shoe size?, the compiler could recognise which parameters were intended according to their data type?
Just to let you know, 4 questions down from this one is another question almost identical. Doing a quick search would have brought up quite a few. Here is a link to the question 4 down from this one:
that question is from 2009, because it has 50 lines with no current answers in them, i thought i would write a new question. if you have 10x 10 line functions with 3 parameters, the solution on that page would take 300 lines of code, compared to 30 lines in unity4... it's easier to edit.
Sorry about that, i didn't really check the dates. It was only 4 questions down so i assumed it was quite recent. In any case, i believe what you're asking is quite simple. The following should provide you with an optional Vector3 parameter:
public void Blah(Vector3 optionalV3 = default(Vector3))
{
//this will default the Vector3 to 0,0,0 when not provided.
}
I'd imagine the same can be accomplished with other types, such as a string:
public void Blah(string optionalS = default(string))
{
//this will create an optional string parameter.
}
So in conclusion, as long as a parameter has a default value, it should become optional, whereas leaving a parameter value free would make it a requirement.
For the sake of completion, here is the $$anonymous$$SDN page with information on the subject:
Named and Optional Arguments (C# Program$$anonymous$$g Guide)
Oh ok, that makes alot of sense, i clearly drank too much at lunch and didnt search for the solution properly, there's a brilliant page here with lots of solutions for JS http://answers.unity3d.com/questions/56502/creating-function-with-default-arguments.html
Answer by frogsbo · Jun 08, 2014 at 03:34 PM
see http://answers.unity3d.com/questions/56502/creating-function-with-default-arguments.html for solutions
Your answer
![](https://koobas.hobune.stream/wayback/20220613151035im_/https://answers.unity.com/themes/thub/images/avi.jpg)