- Home /
Question by
lance12367 · Dec 20, 2015 at 03:37 AM ·
unity 2d
there is an error... help!!
MY CODE the error readz
An object reference is required to access non-static member UnityEngine.Rigidbody2D.velocity' using UnityEngine; using System.Collections;
public class PLAYER_CONTROLLER : MonoBehaviour {
public float moveSpeed;
public float jumpHeight;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown (KeyCode.Space)){
Rigidbody2D.velocity = new Vector2(0,jumpHeight);
}
}
} `
Comment
Best Answer
Answer by allenallenallen · Dec 20, 2015 at 03:40 AM
You need to get the Rigidbody2D component of the object you want to jump.
Rigidbody2D rb;
void Start(){
rb = GetComponent<Rigidbody2D>; // If this script is attached to the object with the Rigidbody2D component, then this is enough.
}
// Replace the same velocity line of code with the following line.
rb.velocity = new Vector2(0, jumpHeight);
Your answer
Follow this Question
Related Questions
App gets stuck on WebGL 0 Answers
Check the position after drag and drop 0 Answers
Play sound once on collision 1 Answer