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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Issue With NullReferneceException

Discussion in 'Scripting' started by jonathanrhcooper, Feb 12, 2021.

  1. jonathanrhcooper

    jonathanrhcooper

    Joined:
    Dec 18, 2020
    Posts:
    15
    I am having an issue that it will not stop givving error in log but game works fine, can someone tell me where this error is? Error.PNG Script Output.PNG
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6. using System;
    7. using System.Linq;
    8. using System.Text;
    9.  
    10. public class AmmoDisplay : MonoBehaviour
    11. {
    12.     public SerialController serialController;
    13.     public int ammo;
    14.     public bool isFiring;
    15.     public Text ammoDisplay;
    16.     public Text scoreDisplay;
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         serialController = GameObject.Find("SerialController").GetComponent<SerialController>();
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         ammoDisplay.text = ammo.ToString();
    27.         if (Input.GetKeyDown(KeyCode.Z) && !isFiring && ammo > 0)
    28.          {
    29.             isFiring = true;
    30.             serialController.SendSerialMessage("3");
    31.             ammo--;
    32.             isFiring = false;
    33.          }
    34.  
    35.             if (Input.GetKeyUp(KeyCode.Z))
    36.             {
    37.               Debug.Log("Sending Release");
    38.               serialController.SendSerialMessage("3");
    39.             }
    40.             if (ammo <= 0)
    41.             {
    42.                 SceneManager.LoadScene(0);
    43.                serialController.SendSerialMessage("4");
    44.             }
    45.         string message = serialController.ReadSerialMessage();
    46.         var chars = message.ToCharArray();
    47.         var message1 = string.Empty;
    48.         foreach (var eachChar in chars)
    49.         {
    50.             if (eachChar != ' ')
    51.             {
    52.                 message1 = message1 + eachChar;
    53.                 scoreDisplay.text = message1.ToString();
    54.             }
    55.         }
    56.     }
    57. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    The answer is always the same... ALWAYS. It is the single most common error ever. Don't waste your life on this problem. 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:

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

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

    jonathanrhcooper

    Joined:
    Dec 18, 2020
    Posts:
    15
    I Understand this I literally have spent 10+ hours on trying to locate this error in my script and I am unable to locate it.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Did you look at what is on the left side of the dot on line 46?

    Based on the error I'm going to bet
    message
    is null.

    Since that's the line number actually listed in the error, until you PROVE to yourself that
    message
    is not null, you shouldn't even consider or even look at anything not directly related to that question.

    This is the question and it should take you less than 60 seconds to write code to answer it:

    "Is
    message
    null in line 46?"

    Test it and find out. Maybe it needs to be guarded with a null check before you blindly start using it.
     
  5. jonathanrhcooper

    jonathanrhcooper

    Joined:
    Dec 18, 2020
    Posts:
    15
    Thanks for the reply I have just noticed that I had it right in one of my scripts but not in the other. Thanks for pointing this out!!!