• 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
Question by marsmission111 · May 27, 2016 at 04:55 PM · c#scripting problemerrorjumpingnewbie

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

Comment

People who like this

0 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 Mmmpies · May 27, 2016 at 05:25 PM 0
Share

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.

HERE

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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.

Comment
someguywithamouse

People who like this

1 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 marsmission111 · May 28, 2016 at 01:18 PM 1
Share

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))]
 [AddComponentMenu("Control Script/FPS Input")]
 public class FPSInput : MonoBehaviour {
     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.ClampMagnitude (movement, speed);

         movement *= Time.deltaTime;
         movement = transform.TransformDirection (movement);
         _charController.Move (movement);
         
         if (isFalling == true)
         {
             movement.y = gravity;
         }
         //bool jump = Input.GetAxis("Jump") != 0;
         if (Input.GetKeyDown("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

avatar image Mmmpies marsmission111 · May 28, 2016 at 01:57 PM 1
Share

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. Might be better to use...

 void OnCollisionExit()
 {
      isFalling = true;
 }
 
 void OnCollisionEnter()
 {
      isFalling = false;
 }
avatar image

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

}

Comment
Mmmpies
someguywithamouse

People who like this

2 Show 4 · 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 marsmission111 · May 28, 2016 at 07:22 AM 0
Share

This is my code now:

 using UnityEngine;
 using System.Collections;

 public class Jump : MonoBehaviour {

     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.

avatar image Mmmpies marsmission111 · May 28, 2016 at 08:53 AM 0
Share

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.

avatar image marsmission111 Mmmpies · May 28, 2016 at 09:51 AM 0
Share

That fixed the error but, my character is still not jumping.

avatar image Mmmpies · May 28, 2016 at 11:16 AM 0
Share

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.GetMouseButtonDown(0))
 {
      // put your jump code here
 }
avatar image

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.

Comment

People who like this

0 Show 3 · 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 Mmmpies · May 28, 2016 at 12:22 PM 0
Share

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.

avatar image marsmission111 Mmmpies · May 28, 2016 at 12:53 PM 0
Share

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

avatar image marsmission111 marsmission111 · May 28, 2016 at 12:59 PM 0
Share

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Error CS0122: inaccessable due to protection level 1 Answer

IEnumerator, Unexpected symbol? 3 Answers

how do I rezolve "look rotation viewing vector is zero"? 1 Answer

Error CS1525: Unexpected symbol 'void' 1 Answer

Can jump even when in the air. 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