Search Unity

Bug Null Exception Unity Learn

Discussion in 'Scripting' started by Flicked, Mar 26, 2023.

  1. Flicked

    Flicked

    Joined:
    Mar 18, 2023
    Posts:
    3
    Hi. I've been trying for over an hour to figure out what is null in my code. The error reads line 16 (hungerSlider.maxValue = amountToBeFed). I found this (http://plbm.com/?p=221) already but I'm still having trouble. I'm not very good at debugging and as far as I can tell, these variables have values. But if that were the case I don't think I'd have a null pointer there.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class AnimalHunger : MonoBehaviour
    7. {
    8.     public Slider hungerSlider;
    9.     public int amountToBeFed;
    10.     private int currentFedAmount = 0;
    11.     private GameManager gameManager;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         hungerSlider.maxValue = amountToBeFed;
    17.         hungerSlider.value = 0;
    18.         hungerSlider.fillRect.gameObject.SetActive(false);
    19.  
    20.         gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.        
    27.     }
    28.  
    29.     public void FeedAnimal(int amount)
    30.     {
    31.         currentFedAmount += amount;
    32.         hungerSlider.fillRect.gameObject.SetActive(true);
    33.         hungerSlider.value = currentFedAmount;
    34.        
    35.         if(currentFedAmount >= amountToBeFed)
    36.         {
    37.             gameManager.AddScore(amountToBeFed);
    38.             Destroy(gameObject, 0.1f);
    39.         }
    40.     }
    41. }
    42.  
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,849
    You probably haven't assigned
    hungerSlider
    in the inspector.
     
  3. Flicked

    Flicked

    Joined:
    Mar 18, 2023
    Posts:
    3
    If this is what you mean, then they're assigned.
    upload_2023-3-25_21-18-8.png
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,849
    Then I have a feeling you have another component somewhere else in the scene without it assigned.

    You can use the second overload of Debug.Log and similar to highlight the object that fired the debug log, as a way to determine where the error is happening:
    Code (CSharp):
    1. private void Start()
    2. {
    3.     if (hungerSlider == null)
    4.     {
    5.         Debug.LogError("Animal Hunger: Hunger Slider is not referenced", this);
    6.     }
    7.     else
    8.     {  
    9.         hungerSlider.maxValue = amountToBeFed;
    10.         hungerSlider.value = 0;
    11.         hungerSlider.fillRect.gameObject.SetActive(false);
    12.     }
    13. }
    This is simple debugging you will need to learn to do on your own.
     
    Flicked likes this.
  5. Flicked

    Flicked

    Joined:
    Mar 18, 2023
    Posts:
    3
    It did indeed trigger as null. I didn't know I could click on the error directly to highlight the object. That is really useful! You were right, there was an unconnected script. I had accidentally added a copy of it in a place it shouldn't have been, and once I had it highlighted it was easy to fix. Thank you so much!
     
    spiney199 likes this.