C
C#15mo ago
Fulfinsen

❔ 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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
public float speed = 10f;
public Vector2 maxVelocity = new Vector2(3, 5);


// Update is called once per frame
void Update()
{
var forceX = 0f;
var forceY = 0f;

var absVelX = Mathf.Abs(Rigidbody2D.velocity.x);


if (Input.GetKey("right"))
{
if (absVelX < maxVelocity.x)
forceX = speed;
}else if (Input.GetKey("left"))
{
if (absVelX < maxVelocity.x)
forceX = -speed;
}

Rigidbody2D.AddForce(new Vector2(forceX, forceY));
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
public float speed = 10f;
public Vector2 maxVelocity = new Vector2(3, 5);


// Update is called once per frame
void Update()
{
var forceX = 0f;
var forceY = 0f;

var absVelX = Mathf.Abs(Rigidbody2D.velocity.x);


if (Input.GetKey("right"))
{
if (absVelX < maxVelocity.x)
forceX = speed;
}else if (Input.GetKey("left"))
{
if (absVelX < maxVelocity.x)
forceX = -speed;
}

Rigidbody2D.AddForce(new Vector2(forceX, forceY));
}
}
11 Replies
Thinker
Thinker15mo ago
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 it
Fulfinsen
FulfinsenOP15mo ago
something like this?
private Rigidbody2D rb2d;


void Start()
{
Rigidbody2D rb2d = GetComponent<Rigidbody2D>();
}
private Rigidbody2D rb2d;


void Start()
{
Rigidbody2D rb2d = GetComponent<Rigidbody2D>();
}
Thinker
Thinker15mo ago
yeah
Fulfinsen
FulfinsenOP15mo ago
now I am getting an error in unity:
NullReferenceException: Object reference not set to an instance of an object
Player.Update () (at Assets/Scripts/Player.cs:23)
NullReferenceException: Object reference not set to an instance of an object
Player.Update () (at Assets/Scripts/Player.cs:23)
and it points me at
var absVelX = Mathf.Abs(rb2d.velocity.x);
var absVelX = Mathf.Abs(rb2d.velocity.x);
Thinker
Thinker15mo ago
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)
Fulfinsen
FulfinsenOP15mo ago
I put a debug.logError to see what is happening and it says that Rigidbody2d is not assigned
Thinker
Thinker15mo ago
yeah because you're not assigning the field
Fulfinsen
FulfinsenOP15mo ago
yeah, there is definitely something that I am missing and I don't see it
Thinker
Thinker15mo ago
Again this line
Rigidbody2D rb2d = GetComponent<Rigidbody2D>();
Rigidbody2D rb2d = GetComponent<Rigidbody2D>();
rb2d is a local variable It gets discarded at the end of the method You want to assign to the rb2d field you've declared
Fulfinsen
FulfinsenOP15mo ago
yeah, you are right I removed the Rigidbody2D and assigned rb2d as needed now it works thanks
Accord
Accord15mo ago
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.
Want results from more Discord servers?
Add your server