Error CS0120 : An object reference is required to access non-static member
In trying to make a jumping script in C#, I ran across this error:
error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody.velocity'
This is my program:
using UnityEngine;
using System.Collections;
public class Jump : MonoBehaviour {
public float JumpHeight = 8.0f;
private bool isFalling = false;
void Update () {
bool jump = Input.GetAxis("Jump") != 0;
if (jump && isFalling == false)
{
Vector3 velocity = Rigidbody.velocity;
velocity.y += JumpHeight;
Rigidbody.velocity = velocity;
isFalling = true;
}
}
void OnCollisionStay ()
{
isFalling = false;
}
}
It would be very helpful if you could help me...
Thanks
You've not told it which Rigidbody you want to apply the velocity to.
Looks at API or manual and you'll see an example.
You know that the rigidbody you want to apply the velocity to is (probably) your player but the script doesn;t unless you tell it.
That example has a public Rigidbody rb so in the inspector drag your Player onto that slot in the script.
Answer by Mmmpies · May 28, 2016 at 01:00 PM
Honestly mate I'd ditch that jump bool and getaxis as it seems to be a really strange way to find out if space is being pressed you really only need this...
if (Input.GetKeyDown("space") && isFalling == false) {
I've converted this to an answer so you can accept as the actual solution is pretty drawn out over a lot of comments. Really I'd upvote the other answer as well, always good to show appreciation if someone has taken time to help out.
Thanks for the help. Still trying to figure out why it does not work in my scene. Does it have to do with that my character is a capsule? Is my movement script interfering with it? Anyway thanks for the help...
P.s.: "This is my movement script and my jump script combined"
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
[AddComponent$$anonymous$$enu("Control Script/FPS Input")]
public class FPSInput : $$anonymous$$onoBehaviour {
public float speed = 6.0f;
public float gravity = -9.8f;
public float JumpHeight = 8.0f;
private bool isFalling = false;
public Rigidbody rig;
private CharacterController _charController;
// Use this for initialization
void Start() {
_charController = GetComponent<CharacterController> ();
}
// Update is called once per frame
void Update () {
float deltaX = Input.GetAxis ("Horizontal") * speed;
float deltaZ = Input.GetAxis ("Vertical") * speed;
//float deltaY = Input.GetAxis ("Jump") * jump;
Vector3 movement = new Vector3 (deltaX, 0, deltaZ);
movement = Vector3.Clamp$$anonymous$$agnitude (movement, speed);
movement *= Time.deltaTime;
movement = transform.TransformDirection (movement);
_charController.$$anonymous$$ove (movement);
if (isFalling == true)
{
movement.y = gravity;
}
//bool jump = Input.GetAxis("Jump") != 0;
if (Input.Get$$anonymous$$eyDown("space") && isFalling == false)
{
//Vector3 velocity = rig.velocity;
//velocity.y += JumpHeight;
//rig.velocity = velocity;
movement.y = JumpHeight;
isFalling = true;
Debug.Log("Jump");
}
//transform.Translate (deltaX * Time.deltaTime, 0, deltaZ * Time.deltaTime);
//transform.Translate (0, speed, 0);
}
void OnCollisionStay ()
{
isFalling = false;
Debug.Log("Grounded");
}
}
Thanks for the help...
marsmission111
There are just so many elements that could be off it's hard to say without seeing the actual scene. Oh and I'd watch that OnCollisionStay. You may end up with a double jump if it's still registering a collision as the jump fires off. $$anonymous$$ight be better to use...
void OnCollisionExit()
{
isFalling = true;
}
void OnCollisionEnter()
{
isFalling = false;
}
Answer by Toon_Werawat · May 28, 2016 at 03:07 AM
Wait.... It has to be this way...
void Update () {
bool jump = Input.GetAxis("Jump") != 0;
if (jump && isFalling == false)
{
Vector3 velocity = GetComponent<Rigidbody>().velocity;
velocity.y += JumpHeight;
GetComponent<Rigidbody>().velocity = velocity;
isFalling = true;
}
}
Or this way
public RigidBody rig;
void Update () {
bool jump = Input.GetAxis("Jump") != 0;
if (jump && isFalling == false)
{
Vector3 velocity = rig.velocity;
velocity.y += JumpHeight;
rig.velocity = velocity;
isFalling = true;
}
}
This is my code now:
using UnityEngine;
using System.Collections;
public class Jump : $$anonymous$$onoBehaviour {
public float JumpHeight = 8.0f;
private bool isFalling = false;
private Rigidbody rig;
void Update () {
bool jump = Input.GetAxis("Jump") != 0;
if (jump && isFalling == false)
{
Vector3 velocity = rig.velocity;
velocity.y += JumpHeight;
rig.velocity = velocity;
isFalling = true;
}
}
void OnCollisionStay ()
{
isFalling = false;
}
}
I now have the error:
warning CS0649: Field Jump.rig' is never assigned to, and will always have its default value
null'
I know it is just a warning but, when I play my scene my character does not jump. Also, could you help me with the "OnCollisionStay" part of my code, it does not work.
Thanks.
Change
private Rigidbody rig;
to
public Rigidbody rig;
Then select your player in the Hierarchy and look in the inspector. You'll see in the script section the there's now a rig reference with a slot next to it. Just drag your player onto that slot.
That fixed the error but, my character is still not jumping.
Throw in some debug.Logs to show the value of that bool. Normally GetAxis is based on Horizontal or Vertical.
if Jump is never true then it'll never run the jump code. For testing you might want to simplify it so your just check for mouse button down rather than your current method...
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
// put your jump code here
}
Answer by marsmission111 · May 28, 2016 at 11:48 AM
Edit: Sorry this not an answer.
I added a "Debug.Log" statement that tells me when I am in the if statement and when I am touching an object.
This is my program:
using UnityEngine;
using System.Collections;
public class Jump : MonoBehaviour {
public float JumpHeight = 8.0f;
private bool isFalling = false;
public Rigidbody rig;
void Update () {
bool jump = Input.GetAxis("Jump") != 0;
if (jump && isFalling == false)
{
Vector3 velocity = rig.velocity;
velocity.y += JumpHeight;
rig.velocity = velocity;
isFalling = true;
Debug.Log("Jump");
}
}
void OnCollisionStay ()
{
isFalling = false;
Debug.Log("Grounded");
}
}
When I press space the first time it prints it out in the console. Any other time it does not work. Also, it never prints out "Grounded". It should when I touch an object. Could you give me a working jumping program or fix mine?
Thanks.
Does the player and whatever you use for ground both have colliders on them. I just created a very simple test scene with you script, a plane for the ground and a cube for the player.
Dragged your script onto the cube and pressed space, it jumped, landed and gave out a load of Grounded messages. Pressed space again and it jumped again.
The only thing I can think is that the collision isn't happening. Or maybe you've one of them set isTrigger?
But the script appears to work even if it's an odd way of checking if Space being pressed.
That has fixed the "Grounded" problem but, my character is still not jumping. I will make a new scene to test the script. I will tell you how it goes...
I am done and the script works... I do not know why it does not work in my other scene. By the way how do I reward you?
Your answer
Follow this Question
Related Questions
Pause menu done exactly according to tutorial doesen't work 2 Answers
Help! Values from previous Serialized List<> shows up again after a while using Coroutine 0 Answers
Instatiated object not being referenced in Start function? 2 Answers
Сonstructor return null 0 Answers
how do I rezolve "look rotation viewing vector is zero"? 1 Answer