Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by SalamanderSultan · Aug 03, 2017 at 09:10 AM · script.2d-platformer2d-physics

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;
             }
         }
     }
 }

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShadoX · Aug 03, 2017 at 09:08 AM 0
Share

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>();

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
             }
         }
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 ..

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SalamanderSultan · Aug 10, 2017 at 10:37 PM 0
Share

@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).

avatar image ketchupinho · Jan 17, 2018 at 03:06 PM 0
Share

11/10 answer, very informative, I can dig that.

@SalamanderSultan I guess we'll never know.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

114 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges