Okay, I am making a video game, and I want to code it so that the sprite in the bottom-left corner of the screen will change under certain conditions. However, it throws “Null Reference Exception: Object reference not set to an instance of an object.” when the sprite is supposed to change. What would I need to change, in order to fix this? Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class HUD : MonoBehaviour
{
//gets the HP display
[SerializeField]
private TextMeshProUGUI HPDisplay;
//gets the scriptable object that holds Kaitlyn's stats
[SerializeField]
private KaitlynSO kaitlyn;
//stores Kaitlyn's icon
public Image icon;
//stores the other character's icon
[SerializeField]
private Image otherIcon;
//stores the text for the speech
[SerializeField]
private TextMeshProUGUI speech;
//gets the player script
[SerializeField]
private Player player;
//stores the text that displays if the game is paused
[SerializeField]
private TextMeshProUGUI pause;
//stores the button for deleting saved data
[SerializeField]
private GameObject deleteButton;
//stores Kaitlyn's base sprite
[SerializeField]
private Sprite BaseSprite;
//stores Kaitlyn's hurt sprite
[SerializeField]
private Sprite HurtSprite;
//stores Kaitlyn's killed sprite
[SerializeField]
private Sprite KilledSprite;
//is called at the start to set the sprites and set the pause menu inactive
private void Awake()
{
icon = GetComponent<Image>();
pause.gameObject.SetActive(false);
deleteButton.gameObject.SetActive(false);
StartCoroutine(ResetSprite());
}
//displays Kaitlyn's stats
private void Update()
{
//icon.sprite = BaseSprite;
HPDisplay.text = "Health: " + kaitlyn.HP + "/" + kaitlyn.maxHP;
//calls the pause menu
if(player.IsPaused == true)
{
pause.gameObject.SetActive(true);
deleteButton.gameObject.SetActive(true);
}
//closes the pause menu
else
{
pause.gameObject.SetActive(false);
deleteButton.gameObject.SetActive(false);
}
}
//switches Kaitlyn's sprite to the hurt sprite
public void hurtSprite()
{
icon.sprite = HurtSprite;
}
//sets Kaitlyn's sprite back to neutral
private IEnumerator ResetSprite()
{
yield return new WaitForSeconds(1f);
icon.sprite = BaseSprite;
}
}
I don’t have time to read through your code, but you stubled into the most basic error here.
Something you are trying to access has no assigned a value. If the sprite change gives you a null ref, check if you have assigned a value to the field you are trying to use. i.e. you are trying to use your HurtSprite, see that the field visible in your Inspector actually has some value assigned. Otherwise it would give a null reference error.
Unrelated to this, your method naming could use some fixing; You call one of your methods hurtSprite, but then a similarly named variable is called HurtSprite. This is an incorrect way, since the methods should use capitalized names aka PascalCase, see:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class HUD : MonoBehaviour
{
//gets the HP display
[SerializeField]
private TextMeshProUGUI HPDisplay;
//gets the scriptable object that holds Kaitlyn's stats
[SerializeField]
private KaitlynSO kaitlyn;
//stores Kaitlyn's icon
public Image icon;
//stores the other character's icon
[SerializeField]
private Image otherIcon;
//stores the text for the speech
[SerializeField]
private TextMeshProUGUI speech;
//gets the player script
[SerializeField]
private Player player;
//stores the text that displays if the game is paused
[SerializeField]
private TextMeshProUGUI pause;
//stores the button for deleting saved data
[SerializeField]
private GameObject deleteButton;
//stores Kaitlyn's base sprite
[SerializeField]
private Sprite BaseSprite;
//stores Kaitlyn's hurt sprite
[SerializeField]
private Sprite HurtSprite;
//stores Kaitlyn's killed sprite
[SerializeField]
private Sprite KilledSprite;
//is called at the start to set the sprites and set the pause menu inactive
private void Awake()
{
icon = GetComponentInChildren<Image>();
pause.gameObject.SetActive(false);
deleteButton.gameObject.SetActive(false);
StartCoroutine(ResetSprite());
}
//displays Kaitlyn's stats
private void Update()
{
//icon.sprite = BaseSprite;
HPDisplay.text = "Health: " + kaitlyn.HP + "/" + kaitlyn.maxHP;
//calls the pause menu
if(player.IsPaused == true)
{
pause.gameObject.SetActive(true);
deleteButton.gameObject.SetActive(true);
}
//closes the pause menu
else
{
pause.gameObject.SetActive(false);
deleteButton.gameObject.SetActive(false);
}
}
//switches Kaitlyn's sprite to the hurt sprite if she takes damage, and the killed sprite if she dies
public void hurtSprite()
{
//checks if Kaitlyn's health is above 0
if(kaitlyn.HP > 0)
{
icon.sprite = HurtSprite;
StartCoroutine(ResetSprite());
}
else
{
icon.sprite = KilledSprite;
StartCoroutine(ResetSprite());
}
}
//sets Kaitlyn's sprite back to neutral
private IEnumerator ResetSprite()
{
yield return new WaitForSeconds(1f);
icon.sprite = BaseSprite;
}
}