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 /
This question was closed Mar 15, 2020 at 08:48 PM by justinsykes2006 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by justinsykes2006 · Mar 15, 2020 at 08:00 PM · warningwarnings

error CS0649

I tried to find how to solve this problem but I just don't get it! It says:"Field 'PlayerControls.rb' is never assigned to, and will always have its default value null", When I click on the error it sends me to line 59 (the last line(rb.velocity = Vector2.zero;)). Please help me I cant figure out the problem, here's my script:


using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class PlayerControls : MonoBehaviour{

 public float moveSpeed = 5f;

 public float jumpForce = 30f;

 bool isJumping;

 Rigidbody2D rb;
 

 // Start is called before the first frame update
 void Start()
 {
 }
 // Update is called once per frame
 void Update()
 {
     Jump();
     Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
     transform.position += movement * Time.deltaTime * moveSpeed;
 }
 void Jump()
 {
     if (Input.GetButtonDown("Jump"))
     {
         gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
     }
 }


 void Jumping()
 {
     if (Input.GetKey(KeyCode.Space) && !isJumping)
     {
         isJumping = true;
         rb.AddForce(new Vector2(rb.velocity.x, jumpForce));
     }
 }

 private void OnCollisionEnter2D(Collision2D other)
 {
     if (other.gameObject.CompareTag("Ground"))
     {
         isJumping = false;

         RbVelocity();
     }
 }

 private void RbVelocity()
 {
     rb.velocity = Vector2.zero;
 }

}

Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by metalted · Mar 15, 2020 at 08:15 PM

Field 'PlayerControls.rb' is never assigned to, and will always have its default value null". You have declared a Rigidbody2D called rb on the top of your script. As the message says, the variable is never assigned. So there isnt a Rigidbody in this variable, it is a null value. Like you do on line 25, you should use GetComponent() and assign it to the rb variable. This is something you want to do in the start function.

 using System.Collections;
 
 using System.Collections.Generic;
 
 using UnityEngine;
 
 public class PlayerControls : MonoBehaviour{
 
  public float moveSpeed = 5f;
  public float jumpForce = 30f;
  bool isJumping;
  Rigidbody2D rb;
  
  // Start is called before the first frame update
  void Start()
  {
     rb = gameObject.GetComponent<Rigidbody2D>();
  }
  // Update is called once per frame
  void Update()
  {
      Jump();
      Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
      transform.position += movement * Time.deltaTime * moveSpeed;
  }
  void Jump()
  {
      if (Input.GetButtonDown("Jump"))
      {
          rb.AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
      }
  }
  void Jumping()
  {
      if (Input.GetKey(KeyCode.Space) && !isJumping)
      {
          isJumping = true;
          rb.AddForce(new Vector2(rb.velocity.x, jumpForce));
      }
  }
  private void OnCollisionEnter2D(Collision2D other)
  {
      if (other.gameObject.CompareTag("Ground"))
      {
          isJumping = false;
          RbVelocity();
      }
  }
  private void RbVelocity()
  {
      rb.velocity = Vector2.zero;
  }


Comment
Add comment · 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 justinsykes2006 · Mar 15, 2020 at 08:20 PM 0
Share

@metalted thanks it works! But now when I jump I can jump as many times as I want but I must only be able to jump once?

avatar image metalted justinsykes2006 · Mar 15, 2020 at 08:23 PM 0
Share

Thats because you are calling the function Jump() from Update(), i think you want to use the Jumping() function for the desired behaviour.

avatar image justinsykes2006 metalted · Mar 15, 2020 at 08:34 PM 0
Share

@metalted I tried this but now I can't even jump, it just does nothing anymore.

Follow this Question

Answers Answers and Comments

123 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

Related Questions

SendMessage cannot be called during Awake, CheckConsistency, or OnValidate 0 Answers

Static function won't call another static function 1 Answer

How do I fix "JobTempAlloc has allocations that are more than 4 frames old" when I'm not actually using Allocator.TempJob? 1 Answer

script isn't loading (beginner question) 1 Answer

Get rid of certain warnings on the console. 1 Answer


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