Help with NullReferenceException: Object reference not set to an instance of an object

Sorry for the newbie question, but I am new and although I’ve tried to work my way through it from reading on the internet I think I need steering in the right direction. I have a circle that follows my cursor, and when it is over another circle on my screen I want to adjust the movement speed of a car. I have this script attached to my cursor circle:

public class Cursor : MonoBehaviour
{

    public int moveSpeed = 1;


    public void Update()
    { }
        void OnTriggerEnter(Collider collider)
        {
            moveSpeed = 5;
            Debug.Log("collide");
        
        }
        void OnTriggerExit(Collider collider)
        {
            moveSpeed = 1;
        }
    }

And this on the the vehicle:

public class RacerMovement : MonoBehaviour
{

    private Cursor collideYesNo;
 
    void Start()
    {
        collideYesNo = GetComponent<Cursor>();
    }

    void Update()
    {
        HandleMovementInput();
    
    }
    void HandleMovementInput()
    {
        float _horizontal = Input.GetAxis("Horizontal");
        float _vertical = Input.GetAxis("Vertical");

        Vector3 _movement = new Vector3(_horizontal, 0, _vertical);
        transform.Translate(_movement * collideYesNo.moveSpeed * Time.deltaTime, Space.World);
    }
}

This doesn’t show any errors in Visual Studio, however when I run the game I get

NullReferenceException: Object reference not set to an instance of an object
RacerMovement.HandleMovementInput () (at Assets/Scripts/RacerMovement.cs:32)
RacerMovement.Update () (at Assets/Scripts/RacerMovement.cs:19)

I imagine it has something to do with my collideYesNo as this is line 32. Any help is greatly appreciated

I think in Start() you need to use gameObject.GetComponent();

You need to learn to wrap your code in code tags in posts. [co de] code listing[/co de]. Only without the spaces in the word code. If I leave them in, it triggers a code listing. It reads easier.

Whenever you get a null ref, look back at where you assign the variable.

Have you tried attaching a debugger?

Sorry about that, i tidied it up. Thank you very much that was exacatly what I wanted!

Glad you’re back in service. To save you some time next time, remember, the answer is always the same… ALWAYS. It is the single most common error ever.

Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception
  • also known as: Object reference not set to an instance of an object

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

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

This is the kind of mindset and thinking process you need to bring to this problem:

Step by step, break it down, find the problem.