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. Dismiss Notice

Object reference not set to an instance of an object. How to fix this

Discussion in 'Getting Started' started by mriindrahi, Jun 15, 2021.

  1. mriindrahi

    mriindrahi

    Joined:
    Apr 13, 2021
    Posts:
    4
    So in unity I came Across this problem NullReferenceException: Object reference not set to an instance of an object PlayerController.ApplyMovement () (at Assets/Scripts/PlayerController.cs:63) PlayerController.FixedUpdate () (at Assets/Scripts/PlayerController.cs:31)

    Can anyone explain how to fix this or better do it for me so i did not screw up.

    Code (CSharp):
    1. using System.Collections;
    2.  
    3. using System.Collections.Generic;
    4.  
    5. using UnityEngine;
    6.  
    7. public class PlayerController : MonoBehaviour {
    8.  
    9. private float movementInputDirection;
    10.  
    11. private bool isFacingRight = true;
    12.  
    13. private Rigidbody2D rb;
    14.  
    15. public float movementSpeed = 10.0f;
    16. public float jumpForce = 16f;
    17.  
    18.  
    19. void start()
    20. {
    21.    rb = GetComponent<Rigidbody2D>();
    22. }
    23.  
    24. void Update()
    25. {
    26.    CheckInput();
    27.    CheckMovementDirection();
    28. }
    29.  
    30. void FixedUpdate()
    31. {
    32.    ApplyMovement();
    33. }
    34.  
    35. private void CheckMovementDirection()
    36. {
    37.    if(isFacingRight && movementInputDirection < 0)
    38.    {
    39.        Flip();
    40.    }
    41.    else if(isFacingRight && movementInputDirection > 0)
    42.    {
    43.        Flip();
    44.    }
    45. }
    46.  
    47. private void CheckInput()
    48. {
    49.    movementInputDirection = Input.GetAxisRaw("Horizontal");
    50.  
    51.    if (Input.GetButtonDown("Jump"))
    52.    {
    53.        Jump();
    54.    }
    55. }
    56.  
    57. private void Jump()
    58. {
    59.    rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    60. }
    61.  
    62. private void ApplyMovement()
    63. {
    64.    rb.velocity = new Vector2(movementSpeed * movementInputDirection, rb.velocity.y);
    65. }
    66.  
    67. private void Flip()
    68. {
    69.    isFacingRight = !isFacingRight;
    70.    transform.Rotate(0.0f, 100.0f, 0.0f);
    71. }
    72.  
    73. }
     
  2. max3210

    max3210

    Joined:
    Apr 19, 2020
    Posts:
    17
    Your start method on line 19 should have a capital S:

    void Start()
     
  3. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    To expand upon max's answer a bit:

    The reason your code threw a NullReference Exception (I assume at line 59 or 64, depending on what key you pressed) is because of the misnaming of the Start method. Capitalization matters in programming, so Start() and start() are not the same thing. What you have creates a valid method just fine, but it's not being called anywhere, and consequently your variable rb isn't being assigned before you attempt to reference a member of it later on. In order for Unity to call its built-in MonoBehavior methods appropriate, they must be named exactly as listed in the documentation.