- Home /
The question is answered, right answer was accepted
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;
}
}
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;
}
@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?
Thats because you are calling the function Jump() from Update(), i think you want to use the Jumping() function for the desired behaviour.
@metalted I tried this but now I can't even jump, it just does nothing anymore.