Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by MrMylad, Feb 8, 2022.

  1. MrMylad

    MrMylad

    Joined:
    Jan 6, 2022
    Posts:
    6
    Hi, so, I have been trying to make my first 3D game and I've been attempting to make a character controller and I've seen a couple other threads however they don't solve my problem

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class Movement : MonoBehaviour
    5. {
    6.     Rigidbody rb;
    7.     [SerializeField] float MovementSpeed = 5f;
    8.     [SerializeField] float JumpHeight = 5f;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.        
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void FixedUpdate()
    17.     {
    18.         //Movement
    19.         if (Input.GetKey(KeyCode.W))
    20.         {
    21.             //Next line has a bug
    22.             rb.AddForce(transform.forward * MovementSpeed);
    23.         }
    24.         if (Input.GetKey(KeyCode.S))
    25.         {
    26.             //Next line has a bug
    27.             rb.AddForce(-transform.forward * MovementSpeed);
    28.         }
    29.         if (Input.GetKey(KeyCode.A))
    30.         {
    31.             //Next line has a bug
    32.             rb.AddForce(-transform.right * MovementSpeed);
    33.         }
    34.         if (Input.GetKey(KeyCode.D))
    35.         {
    36.             //Next line has a bug
    37.             rb.AddForce(transform.right * MovementSpeed);
    38.         }
    39.     }
    40. }
    41.  
    So yeah, and help is appreciated! I've been trying to fix this but I don't see what's wrong, or as I'm new to this c# thingy it's a dumb question



    (Also, I have a RigidBody in the GameObject that this script is in)
     
  2. MrMylad

    MrMylad

    Joined:
    Jan 6, 2022
    Posts:
    6
    And no I'm not gonna use the built-in Character Controller script, it's too complex
     
    Last edited: Feb 8, 2022
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
  4. perf401

    perf401

    Joined:
    Sep 21, 2021
    Posts:
    4
    You will meet nullreference error plenty of times, so you should really check what Kurt Dekker provided. But if you want quick answer...
    You always need to write it like that:

    Code (CSharp):
    1. Rigidbody2D rb;
    2. void Start()
    3. {
    4.    rb = GetComponent<Rigidbody2D>();
    5. }
    Just to get component that you wrote, because compiler doesn't know which object's is Rigidbody. By writing rb = GetComponent<RIgidbody2D>(), you basically saying that "rb" equals to rigidbody2d component, that is attached to a "movement" script's gameobject.
    But you can also drag and drop RB component in inspector.
    Code (CSharp):
    1. [SerializeField] private Rigidbody2D rb;
    With that, you won't need to write GetComponent in script, but you need to make sure that you dragged and dropped Rigidbody2D component in inspector. [SerializeField] basically serializes private field, so you can see it in ispector, but you can also make it public RigidBody rb, and it will do same thing.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
  6. MrMylad

    MrMylad

    Joined:
    Jan 6, 2022
    Posts:
    6
    I know what Serialize field is, I've literally used it in my script! Thanks for your help anyway!
     
  7. MrMylad

    MrMylad

    Joined:
    Jan 6, 2022
    Posts:
    6
  8. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    What line is the error on? Please post the exact error message.
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    Rather than pushing back on that comment, maybe you would like to absorb the first part of the reply which is the solution to your problem. You didn't provide the line the error was on yet someone gave you the solution so kudos to them!
     
    perf401 likes this.