Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity3d c# - Vector3 as default parameter

Tags:

How can we add Vector3 as default parameter for a method? for example:

Void SpawnCube(Vector3 p = new Vector3(0,0,0)){...}

I just tried the line about I got an error:

Expression being assigned to optional parameter `p' must be a constant or default value

I want to customise a function to spawn some game objects that if I did not provide the transform.position, it will go to (0,0,0).

like image 470
sooon Avatar asked May 18 '15 02:05

sooon


People also ask

Can you use C in Unity?

Unity supports the C# programming language natively. C# (pronounced C-sharp) is an industry-standard language similar to Java or C++. In addition to this, many other . NET languages can be used with Unity if they can compile a compatible DLL - see here for further details.

Does Unity use C# or C?

The language that's used in Unity is called C# (pronounced C-sharp). All the languages that Unity operates with are object-oriented scripting languages.

Do you need C for Unity?

You don't need to learn C# to use Unity, but if you want to become a better Unity developer, having a solid understanding of C# will be a huge bonus and increase your market value and confidence with the software. I'd suggest checking out the C# from scratch video tutorials on www.pluralsight.com.

Is C# enough for Unity?

C# is the primary language of . NET, and all of Unity's libraries are built using C# code. To say that C# is the language of Unity is accurate. Unity has made it clear that it considers C# to be the only truly canon language for Unity development.


2 Answers

I know this is already answered but I just want to add other ways to do this. Vector3? p and Vector3 bar = default(Vector3) should do it.

public void SpawnCube(Vector3? p = null)
{
    if (p == null)
    {
        p = Vector3.zero; //Set your default value here (0,0,0)
    }

}

As htmlcoderexe pointed out,

To use p, you have to use p.Value or cast the p back to Vector3 with ((Vector3)p).

For example, to access the x value from this function with the p variable, p.Value.x, or ((Vector3)p).x.


OR

public void SpawnCube(Vector3 bar = default(Vector3))
{
    //it will make default value to be 0,0,0
}
like image 148
Programmer Avatar answered Sep 17 '22 18:09

Programmer


In the general case, you can't. The default arguments are somewhat limited. See this MSDN page.

Each optional parameter has a default value as part of its definition. If no argument is sent for that parameter, the default value is used. A default value must be one of the following types of expressions:

  • a constant expression;

  • an expression of the form new ValType(), where ValType is a value type, such as an enum or a struct;

  • an expression of the form default(ValType), where ValType is a value type.

In the specific case you posted however, I suspect that new Vector3() will be equivelent to new Vector3(0,0,0), so you may be able to use that instead.

If you need a non-zero default value, you may be able to use method overloading instead.

like image 25
Jason Watkins Avatar answered Sep 19 '22 18:09

Jason Watkins