Unity Nullreferenceexception: Object Reference not set to an instance of an Object Player.Update() (at Assets/Scripts/Player.Cs:22)

Asked

Viewed 27 times

-3

I am new to Unity and have already written the code and according to the tutorial I saw but not solved

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    public float ForceMultiplier = 3f;
    public float maximumVelocity = 3f;


    void Start()
    {

    }

    
    void Update()
    { 
      var HorizontalInput = Input.GetAxis("Horizontal");
      if (GetComponent<Rigidbody>().velocity.magnitude <= maximumVelocity){

        GetComponent<Rigidbody>().AddForce(new Vector3(HorizontalInput * ForceMultiplier, 0, 0));


      }
    }
    
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Hazard"))
        {
          Destroy(gameObject);
        }
    }

}

2 answers

2

Make a check if the Gameobject where your script is intended has the Rigidbody component.

After doing this check and confirming that it is a component in Gameobject, target Getcomponent in Start as follows...

private Rigidbody rb;

void Start(){
   rb = GetComponent<Rigidbody>();
}

When Gameobject is active on scene it will search for the component Rigidbody within the parent component.

If there is a need to query a Rigidbody in a child Gameobject, you will need to use the following code.

private Rigidbody rb;

void Start(){
   rb = GetComponentInChildren<Rigidbody>();
}

Another way is to leave this variable as public and link it directly by the inspector, dragging the Gameobject where it contains Rigidbody, this way it will not be necessary to give a Getcomponent by the script.

After having these steps in mind we go to your code...

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    public float ForceMultiplier = 3f;
    public float maximumVelocity = 3f;
    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    
    void Update()
    { 
      var HorizontalInput = Input.GetAxis("Horizontal");
      if (rb.velocity.magnitude <= maximumVelocity){

        rb.AddForce(new Vector3(HorizontalInput * ForceMultiplier, 0, 0));


      }
    }
    
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Hazard"))
        {
          Destroy(gameObject);
        }
    }

}

0

First I recommend you not to use Getcomponent. Instead, declare the variable of the type you want, example : You are giving a Getcomponent in the Rigidbody of the object in which this class is, so do [Serializefield] private Rigidbody Rb = default;. When saving this code, Unity will show up a field asking "Which Rigidbody object do I get ?" , you drag the object that has the rigidbody to this place and save it. In the code, instead of using a Getcomponent().velocity.magnitude you will use a Rb.velocity.magnitude. The advantages of doing this is that your code will not be searching for the object that has this component several times, even more than you are calling in Update, Update will fetch 30 or 60 times per second this object.

I will redo your code as an example:

public class Player : MonoBehaviour
{
    public float ForceMultiplier = 3f;
    public float maximumVelocity = 3f;
    [SerializeField] private Rigidbody rb = default;

    void Start()
    {

    }

    
    void Update()
    { 
      var HorizontalInput = Input.GetAxis("Horizontal");
      if (rb.velocity.magnitude <= maximumVelocity){

        rb.AddForce(new Vector3(HorizontalInput * ForceMultiplier, 0, 0));


      }
    }
    
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Hazard"))
        {
          Destroy(gameObject);
        }
    }

}

[Serializefield] serves to make a private variable visible in the editor (Unity).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.