So currently I’m making inventory functions for my Michael game. I was making a script called RespawnItem.
public class RespawnItem : MonoBehaviour { //----SERIALIZED PRIVATES----// [SerializeField] private Transform playerLocation; [SerializeField] private Image img; private void Awake() { img = GetComponent<UnityEngine.UI.Image>(); } public void Respawn() { if (img.overrideSprite = gameObject.GetComponent<BetterItemScript>().image) { //set object location to player transform.position = playerLocation.position; //enable the object gameObject.SetActive(true); } } }
I found a null reference exception in
img.overrideSprite = gameObject.GetComponent<BetterItemScript>().image
where the variable img
is null.
I used the Debug session to see what was up and found the issue in img = GetComponent<UnityEngine.UI.Image>();
I looked up and used an if statement so I can properly assign the image to the variable, but it was still found to be null. Here, I thought that the GetComponent
was not properly assigned to the Image variable, so I checked it with a debug session and now the img
variable was no longer null. But then, when I turned off the session, it was still null.
What can I do about this issue?