Unity code problem

Hello, im completing the Complete C# Unity Game Developer 2D course, im on the second game and for some reason when i try to add the boost it says “NullReferenceException: Object reference not set to an instance of an object PlayerController.RespontToBoost”

This is the code:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class PlayerController : MonoBehaviour

{

[SerializeField] float torqueamount = 1f;

[SerializeField] float speedamount = 30f;

[SerializeField] float basespeed = 20f;

   

Rigidbody2D rb2d;

SurfaceEffector2D surfaceEffector2D;

void Start()

{

    rb2d = GetComponent<Rigidbody2D>();

}

void Update()

{

RotatePlayer();

RespondToBoost();

}

void RespondToBoost()

{

    if (Input.GetKey(KeyCode.UpArrow))

    {

       

        surfaceEffector2D.speed = speedamount;

    }else

    {

        surfaceEffector2D.speed = basespeed;

    }

}

   

void RotatePlayer()

{

 if (Input.GetKey(KeyCode.LeftArrow))

    {

        rb2d.AddTorque(torqueamount);

    }

    else if (Input.GetKey(KeyCode.RightArrow))

    {

        rb2d.AddTorque(-torqueamount);

    }

}    

}

Hi TobiSen,

NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see to which line in your code it is referring. If you exposed a field in the Inspector, make sure that it’s not empty.


See also:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms