I’m quite a noob at scripting, and for days i’ve been working on this script to make my character move and jump (2D platformer game). After long hours of understanding the errors i got, there is only one left, and i’m breaking on it.
I got the script from several tutorials, customising it by myself, and here is the erro message i get :
“Assets/Scripts/PlayerScript.cs(43,29): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody2D.AddForce(UnityEngine.Vector2)'”
The point that confuses me is that the error is talking about Vector2, whereas i only mention Vector3 in my script. I assume that there has to be a little essential thing that ican’t see, maybe you could say me what.
I put you my code
Thanks by advance!
using UnityEngine;
using System.Collections;
/// <summary>
/// Player controller and behavior
/// </summary>
public class PlayerScript : MonoBehaviour
{
public int horizontalSpeed = 1;
public int verticalSpeed = 1;
public int rotationSpeed = 20;
public int jumpSpeed = 100;
public float gravity = 14.0F;
private Vector3 moveDirection = Vector3.zero;
private Rigidbody2D controller;
void Start ()
{
controller = gameObject.GetComponent<Rigidbody2D> ();
}
void Update()
{
// fully control character
float horizontalDirection = Input.GetAxis("Horizontal") * horizontalSpeed;
float forwardDirection = Input.GetAxis("Vertical") * verticalSpeed;
moveDirection = new Vector3(horizontalDirection, 0, forwardDirection);
// get jump event
if (Input.GetKey(KeyCode.Space))
{
moveDirection.y = jumpSpeed;
}
Rigidbody2D.AddForce(Vector3.up * Time.deltaTime);
}
}
The reason you were getting an error is because Rigidbody2D is a type, controller is the variable, you need to access a declared rigidbody to use AddForce();
Thank you guys. The scripts had no errors with Exalia’s solution, i realised i did’nt put the if function so my character will move with right and left arrow. I added the function, and now my script has no fatal error, but my character is not moving at all. An idea of what’s going wrong in here?
using UnityEngine;
using System.Collections;
/// <summary>
/// Player controller and behavior
/// </summary>
public class PlayerScript : MonoBehaviour
{
public int horizontalSpeed = 1;
public int verticalSpeed = 1;
public int rotationSpeed = 20;
public int jumpSpeed = 100;
public float gravity = 14.0F;
private Vector2 moveDirection = Vector2.zero;
private Rigidbody2D controller;
void Start ()
{
controller = gameObject.GetComponent<Rigidbody2D> ();
}
void Update()
{
// fully control character
if (Input.GetKey (KeyCode.RightArrow))
{
float horizontalDirection = Input.GetAxis ("Horizontal") * horizontalSpeed;
moveDirection = new Vector2 (horizontalDirection, 0);
}
if (Input.GetKey (KeyCode.LeftArrow))
{
float horizontalDirection = Input.GetAxis ("Horizontal") * horizontalSpeed;
moveDirection = new Vector2 (horizontalDirection, 0);
}
// get jump event
if (Input.GetKey(KeyCode.Space))
{
moveDirection.y = jumpSpeed;
float forwardDirection = Input.GetAxis("Vertical") * verticalSpeed;
}
}
void FixedUpdate ()
{
controller.AddForce (Vector2.up * Time.deltaTime);
}
}
Well, the easier and more mature solution would be to browse the Asset Store for the 2D example project. All you have to do is export it into a UnityPackage and include dependencies, or just import it straight into your project and utilize the scripts attached with the project. They are fully commented and documented in code, and so you can learn from it and modify it as you please.