- Home /
How to add a Object Reference
Hey there,
I'm pretty new to this Unity stuff and C#, I'm still learning. I've had a google, but I've had no luck finding what I need. Basically, I'm having an issue in line 22. The issue is as follows,
error CS010: An object reference is required to access non-static member `UnityEngine.Rigidbody.AddForce(UnityEngine.Vector3, UnityEngine.ForceMode)'
I understand what it means I think, but unsure how I can actually fix the issue. Below, is my entire script and I hope you guys can help me out. :) Thanks! Also would be great, if you can spot any other issues..
using UnityEngine;
using System.Collections;
public class Jump : MonoBehaviour {
//Controls Player Jump Height
private int jumpHeight = 500;
void Start () {
}
void Update () {
if (Input.GetButtonDown("Jump") || Input.GetKey ("w")) {
DoJump();
}
}
void DoJump (){
Rigidbody.AddForce(new Vector3(0, jumpHeight,0), ForceMode.Force);
}
}
Answer by pako · Dec 13, 2014 at 12:14 PM
You should use:
rigidbody.AddForce(new Vector3(0, jumpHeight,0), ForceMode.Force);
i.e. rigidbody with lower case 'r' to refer to the Rigidbody component attached to the player GameObject.
Otherwise Unity doesn't know to which Rigidbody instance you are referring to.
Thank you very much! I'm wondering, if rigidbody.AddForce only for the 3D rigid body, how would I go about using a 2D rigidbody?
I tried rigidbody2d.AddForce without any luck.. :(
I've never used the 2D version, so I don't have experience with this function, but it seems strange that it doesn't work. However, make sure that you are using a Vector2 for the force parameter, rather than a vector3 that you use in the 3D version.
Also consider asking this as a different question so that you get a response from someone who is experienced with this particular function.
Thank you for the advice.. I've made another question :)