C
C#9mo 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
Thinker9mo 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
Fulfinsen9mo ago
something like this?
private Rigidbody2D rb2d;


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


void Start()
{
Rigidbody2D rb2d = GetComponent<Rigidbody2D>();
}
Thinker
Thinker9mo ago
yeah
Fulfinsen
Fulfinsen9mo 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
Thinker9mo 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
Fulfinsen9mo ago
I put a debug.logError to see what is happening and it says that Rigidbody2d is not assigned
Thinker
Thinker9mo ago
yeah because you're not assigning the field
Fulfinsen
Fulfinsen9mo ago
yeah, there is definitely something that I am missing and I don't see it
Thinker
Thinker9mo 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
Fulfinsen9mo ago
yeah, you are right I removed the Rigidbody2D and assigned rb2d as needed now it works thanks
Accord
Accord9mo 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
More Posts
❔ has anyone got any ideas for a program for like a begnniner with like a ui like raylib?i am learning c# an i want to get a idea for a basic thing i can make with like raylib a graphics li❔ TextBox ValidationRule error display issue with ToolTip [XAML, WPF, C#]Hello everyone. I have a custom control which is just a textbox with a tooltip and some validation ❔ Locking of serialized inventoryHi, me and a colleague are discussing the topic of locking. In my mind locking is just a simple mean✅ Filtering problems in .Net CoreHello everyone, I'm in the midst of a coding project and have encountered a challenge. I'm attempti❔ need help , data will not save when I select pdf to uploadsave button ```C# private void btnAddnew_Click(object sender, EventArgs e) { t❔ How do I implement this property?I'm trying to use a C# Notion API wrapper, I've got my code working but for the love of god I cannot❔ How to set code expression value in web.config settings.I want to set a c# code in web.config value like below. The value should be used by two different pr❔ Data breakpoints in MSVSHi, am having problems setting up data breakpoints (break when variable changes) in MSVS for C#. I'v❔ Blazor - Get objects from child componentsI have a page called "FactorSearch", that calls 2 instances of the same component, called "scope". S❔ Please help with a simple train reservation simulator (I'm a beginner).In this train reservation simulator it's supposed to have a maximum capacity of 40 passengers on boa