Hi Everyone, when i add this following script, i got an error message like this :
Assets/Script/BirdController.cs(17,37): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody2D.velocity’
Any help would be appreciated. Thank You.
Below are my scripts :
using UnityEngine;
using System.Collections;
public class BirdController : MonoBehaviour {
public Vector2 jumpForce = new Vector2 (0,200);
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//loncat
if(Input.GetMouseButtonDown(0)){
Rigidbody2D.velocity = Vector2.zero;
Rigidbody2D.AddForce(jumpForce);
}
}
}
The issue you’re having on lines 17 and 18 is that you’re using a Type when you should be using the property.
Rigidbody2D = Type
rigidbody2D = Property derived from MonoBehaviour and it’s stack.
Also the assumption is this script is attached to a GameObject that actually has a Rigidbody2D component on it.
You change the case on the two lines to fix it. Scripting is case sensitive.
Rigidbody2D.velocity = Vector2.zero;
Rigidbody2D.AddForce(jumpForce);
This method used to be ok but has since changed. You now must reference the Rigidbody2D object before you use it.
Try using something like this:
private Rigidbody2D rb2d;
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
if (Input.GetButtonDown('Jump'))
{
rb2d.velocity = Vector2.zero;
rb2d.AddForce(jumpForce);
}
}