Trying to set an object's velocity - can someone explain this error

For this code:
GetComponent().velocity.x = 15;

I get thid error:
Unexpected >.

Thanks

I can see two issues.

  1. GetComponent - you need to tell it which type. In this case I’ll assume you want to get the Rigidbody. So use: GetComponent<RigidBody>().velocity
  2. velocity is immutable, so you can’t change any of it’s variables. What you can do, is set the entire velocity vector: Vector3 origVelocity = GetComponent<RigidBody>().velocity; GetComponent<RigidBody>().velocity = new Vector3(15, origVelocity.y, origVelocity.z);