Problem with 2D jumping script (Player won't leave ground)
Hi, I'm new to Unity and C#, and I'm trying to make a simple script for 2D jumping. Right now, I'm not trying to tie in any animation or anything, just trying to get my player gameObject to jump when I press the corresponding key.
I get the following error message: "NullReferenceException: Object reference not set to an instance of an object JumpScript.FixedUpdate () (at Assets/scripts/JumpScript.cs:27)"
...and the following line of code is highlighted: "rb.AddForce(Vector2.up * jumpHeight);"
My script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JumpScript : MonoBehaviour
{
public float jumpHeight;
public bool isGrounded;
Rigidbody2D rb;
void Start()
{
this.GetComponent<Rigidbody2D>();
}
void OnCollisionEnter2D(Collision2D coll)
{
isGrounded = true;
}
void FixedUpdate()
{
if (Input.GetKeyDown(KeyCode.W))
{
if (isGrounded)
{
rb.AddForce(Vector2.up * jumpHeight);
isGrounded = false;
}
}
}
}
Looks like your "rd" variable is Null, so you have to set it to something. Chances are that you meant to do
rd = this.GetComponent<Rigidbody2D>();
or if the GameObject doesn't have a Rigidbody2D in the first place
rd = this.AddComponent<Rigidbody2D>();
Answer by SalamanderSultan · Aug 15, 2017 at 09:29 PM
I managed to make my player object jump with the following script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JumpScript : MonoBehaviour
{
public bool isGrounded;
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "Ground")
{
isGrounded = true;
}
}
void FixedUpdate()
{
if (Input.GetKeyDown(KeyCode.W))
{
if (isGrounded == true)
{
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 5), ForceMode2D.Impulse);
isGrounded = false;
}
}
}
}
Answer by Wealou · Aug 04, 2017 at 06:10 PM
@SalamanderSultan : Your problem is in the OnCollisionEnter2D .. You have to firste check your colliders and Rigidbody2d in the scene. the jumping part works fine ..
@Wealou : When you say "You have to firste check your colliders and Rigidbody2d in the scene", do you mean I have to check an option on the collider2D / rigidbody2D components? Or just check to make sure that the collider and rigidbody components are attached to the involved gameObjects (the ground and the player)?
I double-checked to make sure that all the right components were attached to my gameObjects (the "player" gameobject has its collider2D and rigidbody2D, the "ground" gameobject has its collider2D, but no rigidbody2D because that caused it to fall with the player object).
11/10 answer, very informative, I can dig that.
@SalamanderSultan I guess we'll never know.
Your answer

Follow this Question
Related Questions
Changing gravity scale through collisions 0 Answers
how do I make 2 player sprites not interact? 0 Answers
Player is bouncing when colliding with wall object 0 Answers
Built game doesn't work as the game preview 0 Answers
Ideas to create ropes like on Rayman 0 Answers