Question by
jayluvsdivn · Aug 17, 2016 at 01:26 PM ·
error message
Error Message
an object reference is required to access non static member error. I'm quite new to unity and c# so I'm sure I'm just not referencing it properly or something but can someone please tell me what I'm doing wrong? I'm trying to add force to a rigidbody.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public GameObject player;
private static Rigidbody2D rb2d;
public int speed;
void Start ()
{
Rigidbody2D rb2d = gameObject.GetComponent<Rigidbody2D>();
speed = 0;
player = this.gameObject;
}
void Update()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector2 moveDirection = new Vector2 (moveHorizontal, moveVertical);
}
public void AddForce(){
Rigidbody2D rb2d = Rigidbody2D.AddForce(moveDirection, ForceMode2D.Force);
}
}
Comment
Answer by jayluvsdivn · Aug 17, 2016 at 02:48 PM
@verzoto that is static by default, I just put that there for my benefit, how do I make it not static if its default static
I don't understand what you're saying. To make it not static just change it on your script to
Rigidbody2D rb2d;
Answer by Verzoto · Aug 17, 2016 at 02:07 PM
Hello!
You need to remove static from your Rigidbody2D rb2d variable. And you don't need to declare variable type everytime, just declare the type when you create the variable.
Your answer
![](https://koobas.hobune.stream/wayback/20220612090954im_/https://answers.unity.com/themes/thub/images/avi.jpg)