NullReferenceException, al the other methods don't work ) :

can someone please help me, im stuck now for 2 days and I can’t find what’s wrong
this is my code.

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor.Callbacks;
using UnityEngine;
using UnityEngine.Scripting.APIUpdating;

public class Player : MonoBehaviour
{
    

    [SerializeField] private float moveSpeed = 7.5f;
    [SerializeField] private bool Start_working = false;

    private Rigidbody2D rb;
    
    private float moveInput;

    // Start is called before the first frame update
    void Start()
    {
        
    }
    

    // Update is called once per frame
    void Update()
    {
        rb = GetComponent<Rigidbody2D>();
        Move();
    }

    private void Move()
    {
        moveInput = UserInput.instance.moveInput.x;

        rb.velocity = new Vector2(moveInput * 7.5f, rb.velocity.y);
    }
}

I’m new so idk if there are already resource to solve this problem, but I couldn’t find it.

Lots of someone’s can help but I’ll point out that you (seem) to be missing a fundamental understanding of what components are. Not in a bad, bad way but in a “you need to understand this” sort of way.

Ask yourself “what is a Rigidbody” and why do I need a reference to it? Why would I need to get this rb component “once per frame”? Could it possibly be different from frame to frame?

You might question all those using statements (at the top) as well. Are they there for a reason or just obscuring what is needed for no apparent benefit?

In any case, for now, define the Rigidbody as a serialized field (like your other ones) and assign it in the inspector.

But think about what the code is doing.

2 Likes

The answer is always the same… ALWAYS!

How to fix a NullReferenceException error

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

NullReference is the single most common error while programming. Fixing it is always the same.

Some notes on how to fix a NullReferenceException error in Unity3D:

http://plbm.com/?p=221

As tley points out, the above has a lot of random misuse issues.

You may wish to start with another tutorial, any one of the Unity ones.

Make sure you don’t just do them without understanding them, or you’re wasting your time.

Two steps to tutorials and / or example code:

  1. do them perfectly, to the letter (zero typos, including punctuation and capitalization)
  2. stop and understand each step to understand what is going on.

If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.

Imphenzia: How Did I Learn To Make Games:

1 Like

Thank you, about configuring the Rigidbody, I tested if the Start method worked, but I didn’t chance it back after testing. But I wil try to define it as an serialized field. brb.

Okay here is my up-to-date code.

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor.Callbacks;
using UnityEngine;
using UnityEngine.Scripting.APIUpdating;

public class Player : MonoBehaviour
{
    

    [SerializeField] private float moveSpeed = 7.5f;
    [SerializeField] private bool Start_working = false;

    [SerializeField] private Rigidbody2D rb;
    
    private float moveInput;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }
    
    

    // Update is called once per frame
    void Update()
    {
        Move();
    }

    private void Move()
    {
        moveInput = UserInput.instance.moveInput.x;

        rb.velocity = new Vector2(moveInput * 7.5f, rb.velocity.y);
    }
}

@Kurt-Dekker I’m trying to do the first step of identify what is null, but cant find it, this is the error, maybe you know what’s wrong. thx tho ( :

Line 35 above is related to input, not rigidbody.

This is why Step 1 find what item is so critical. :slight_smile:

Review how you set up the input system. Apparently you have missed a step.

The Unity tutorials for such basic functions as this are going to be your go-to

Like I said above, you can’t miss anything. Software is 100% or nothing.

1 Like

Omg, I traced it back and, yes I forgot to add the script to the object. I’m so stupid!!!


Thank you for helping and the fast response, people like you, that help newbies like me, are amazing. Thanks!

Before you move forward with your “game” let me suggest that you move forward with an exploration of what you already have at your disposal and what you gain by adding Debug statements to your code.

Your tools showed the error and a trace of how it got to the error and the exact line where the error occurred. If you get in the habit of adding Debug statements you can determine if references are null, and if values are what you believe them to be.

Don’t drive the car until you know where the brake pedal is and how to use it. :slight_smile:

1 Like