rigidbody2D.velocity = Vector2(5,0); Not Working

So I have this line of code:

void Update() {

    rigidbody2D.velocity = Vector2(5,0);

}

That wont work but this will:

void Update() {

    rigidbody2D.velocity = Vector2.right;

}

This is the error I get:

error CS0119: Expression denotes a type', where a variable’, value' or method group’ was expected

I need help correcting my errors. Thank you.

try:

rigidbody2D.velocity = new Vector2(5,0);

You need to use the “new” keyword to intialize a new Vector2 object.

from

rigidbody2D.velocity = Vector2(5,0);

to

rigidbody2D.velocity = new Vector2(5,0);

You’re using C#. When declaring a new object you need to use the ‘new’ keyword.

Vector2 velocity = new Vector2();

Just see it as every time you need a new object you need to use the new keyword.

void Update() 
{
    //You didn't ask for a new object, this is your error.
    rigidbody2D.velocity = Vector2(5,0);

    //You need to 'ask' for it with the new keyword
    rigidbody2D.velocity = new Vector2(5, 0);

}

Vector2.right is a property of the Vector2 class, it’s already been initiated so it’s not new. So you don’t need to use the new keyword for it.