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): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class AnimalHunger : MonoBehaviour { public Slider hungerSlider; public int amountToBeFed; private int currentFedAmount = 0; private GameManager gameManager; // Start is called before the first frame update void Start() { hungerSlider.maxValue = amountToBeFed; hungerSlider.value = 0; hungerSlider.fillRect.gameObject.SetActive(false); gameManager = GameObject.Find("GameManager").GetComponent<GameManager>(); } // Update is called once per frame void Update() { } public void FeedAnimal(int amount) { currentFedAmount += amount; hungerSlider.fillRect.gameObject.SetActive(true); hungerSlider.value = currentFedAmount; if(currentFedAmount >= amountToBeFed) { gameManager.AddScore(amountToBeFed); Destroy(gameObject, 0.1f); } } }
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): private void Start() { if (hungerSlider == null) { Debug.LogError("Animal Hunger: Hunger Slider is not referenced", this); } else { hungerSlider.maxValue = amountToBeFed; hungerSlider.value = 0; hungerSlider.fillRect.gameObject.SetActive(false); } } This is simple debugging you will need to learn to do on your own.
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!