❔ 2D player movement
Hello. I am following an Unity course for making a 2D game and I am at the part where I need to make my player move. I have wrote the code, but I get the same error on 2 lines of code: Cannot access non-static method blank in static context. This is the code (I am getting that error on var absVelX = Mathf.Abs(Rigidbody2D.velocity.x); at velocity and Rigidbody2D.AddForce(new Vector2(forceX, forceY)); at AddForce:
11 Replies
Rigidbody2D.velocity.x
velocity
is an instance member, not a static one
Same with AddForce
You need a reference to the Rigidbody2D
component in order to do anything to itsomething like this?
yeah
now I am getting an error in unity:
and it points me at
Does the player have a
Rigidbody2D
component?
oh wait nvm
Rigidbody2D rb2d = GetComponent<Rigidbody2D>();
you're assigning the component to a local variable, not to the field you declared
(the variable just has the same name as the field)I put a debug.logError to see what is happening and it says that Rigidbody2d is not assigned
yeah because you're not assigning the field
yeah, there is definitely something that I am missing and I don't see it
Again this line
rb2d
is a local variable
It gets discarded at the end of the method
You want to assign to the rb2d
field you've declaredyeah, you are right
I removed the Rigidbody2D and assigned rb2d as needed
now it works
thanks
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.