Search Unity

Resolved I Keep getting this error when I run my game.

Discussion in 'Scripting' started by nsteele1031, Jan 9, 2022.

  1. nsteele1031

    nsteele1031

    Joined:
    Jan 7, 2022
    Posts:
    18
    I keep getting this error code when I run my game and it runs just fine the way I want it I just want to get rid of the error.

    This is the error
    NullReferenceException: Object reference not set to an instance of an object
    PlayerController.Update () (at Assets/Scripts/PlayerController.cs:76)

    This is the script that looks like is making the error. This is the script to control the player movements and edit a text to show health.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     //Player Movement Varables
    9.     private float speed = 5f;
    10.     private float sprintingSpeed = 10f;
    11.     private Rigidbody playerRb;
    12.     public float jumpHight = 10f;
    13.     public bool isOnGround = true;
    14.     public bool isSprinting = true;
    15.  
    16.     //Random varables
    17.     public Clones clone;
    18.  
    19.     //Health varables
    20.     private float health = 100f;
    21.     private string healthString;
    22.     public Text healthUI;
    23.     // Start is called before the first frame update
    24.     void Start()
    25.     {
    26.         clone = gameObject.GetComponent<Clones>();
    27.         playerRb = GetComponent<Rigidbody>();
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         healthString = health.ToString();
    34.         healthUI.text = healthString;
    35.  
    36.         if (Input.GetKey(KeyCode.W))
    37.         {
    38.             if (isSprinting == true)
    39.             {
    40.                 transform.Translate(Vector3.forward * Time.deltaTime * sprintingSpeed);
    41.             }
    42.             else
    43.             {
    44.                 transform.Translate(Vector3.forward * Time.deltaTime * speed);
    45.             }
    46.         }
    47.         if (Input.GetKey(KeyCode.S))
    48.         {
    49.             transform.Translate(Vector3.forward * Time.deltaTime * -speed);
    50.         }
    51.         if (Input.GetKey(KeyCode.D))
    52.         {
    53.             transform.Translate(Vector3.right * Time.deltaTime * speed);
    54.         }
    55.         if (Input.GetKey(KeyCode.A))
    56.         {
    57.             transform.Translate(Vector3.right * Time.deltaTime * -speed);
    58.         }
    59.         if (Input.GetKeyDown(KeyCode.Space) && isOnGround)
    60.         {
    61.             playerRb.AddForce(Vector3.up * jumpHight, ForceMode.Impulse);
    62.             isOnGround = false;
    63.         }
    64.         if (Input.GetKey(KeyCode.LeftShift))
    65.         {
    66.             isSprinting = true;
    67.         } else
    68.         {
    69.             isSprinting = false;
    70.         }
    71.         if (Input.GetKeyDown(KeyCode.C) && clone.on)
    72.         {
    73.             clone.on = false;
    74.         }else
    75.         {
    76.             clone.on = true;
    77.         }
    78.     }
    79.  
    80.     private void OnCollisionEnter(Collision collision)
    81.     {
    82.         isOnGround = true;
    83.     }
    84. }
    85.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    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.

    You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

    - drag it in using the inspector
    - code inside this script initializes it
    - some OTHER external code initializes it
    - ? something else?

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

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

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

    Here is a clean analogy of the actual underlying problem of a null reference exception:

    https://forum.unity.com/threads/nul...n-instance-of-an-object.1108865/#post-7137032
     
  3. nsteele1031

    nsteele1031

    Joined:
    Jan 7, 2022
    Posts:
    18
    Thank you I'm new to unity. I didn't realize that the problem is such a simple thing
     
    Kurt-Dekker likes this.