24

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).

4 Answers 4

32

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
}
6
  • Do that means if I did not put in any value, it will go to Vector3(0,0,0)?
    – sooon
    Commented May 18, 2015 at 3:35
  • yes. try it yourself with debug.log and call the function without passing it any value.
    – Programmer
    Commented May 18, 2015 at 4:03
  • If you go with Vector3?, you'll need to cast it or use p.Value, otherwise you get a type error. Commented Sep 4, 2017 at 9:24
  • Yes, that's right. I have modified my answer to make that clear.
    – Programmer
    Commented Sep 4, 2017 at 9:54
  • Another way to get back to a Vector3 from a Vector3? is like: Vector3 bar = foo ?? Vector3.zero where Vector3.zero is what will be used if it's null.
    – Norgg
    Commented Jun 25, 2020 at 15:52
20

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.

3
  • 2
    You can. See my answer below. stackoverflow.com/a/47420932/8512294
    – Dalton
    Commented Jul 20, 2019 at 19:57
  • 2
    Using the default constructor is covered by my original answer above... Commented Jul 21, 2019 at 17:14
  • 1
    Oh yes, so it does. I must have stopped reading after "You can't"
    – Dalton
    Commented Aug 27, 2019 at 15:14
7

Hi I just ran into this issue where I needed the Vector3 to be optional. But it would keep saying i need a compile time constant. To get around this issue I used this :

    public void myMethod(Vector3 optionalVector3 = new Vector3())
    {
        //you method code here...
    }
7

As a workaround You can overload method.

INSTEAD THIS

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

USE THIS

void SpawnCube(Vector3 p)
{
  //Implementation
}

//overloaded method without parameter which calls SpawnCube with given default parameter
void SpawnCube()
{
  SpawnCube(new Vector3(0,0,0));
}

You've got one implementation of SpawnCube method body and you can use it with or without parameter :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.