NullRefernceExeption

Ive been having an issue with a null reference exception. I am trying to make inventory UI for my game and have it set up so when the user clicks the inventory slot it will enable the other UI elements such as a larger display of the item, a stats window and some buttons for actions such as drop, repair, ect. I have all of the UI elements parented to the background and referenced through a public field. It seems to work fine except when the function to enable the UI elements is refernced from another script then I get the error. I will attach the scripts below

SLOT SCRIPT

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Slot : MonoBehaviour
{
    public StoredItems storedItem;


    public GameObject inventoryBackground;
     InventoryUI ui;

    public Button button;

    public Image slotItemIcon;

    // Start is called before the first frame update
    void Start()
    {
        ui = inventoryBackground.GetComponent<InventoryUI>();
        slotItemIcon.sprite = storedItem.item.sprite;
        button = GetComponent<Button>();
    }

    private void Update()
    {
      
    }

   public void SlotSelected(Slot slot)
    {
        Debug.Log(slot.storedItem);

        ui.NewSelectedItem();
    }

}

UI SCRIPT
```csharp
**using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class InventoryUI : MonoBehaviour
{

public GameManager gameManager;

public GameObject itemIcon;
public GameObject itemStats;
public GameObject buttonA;
public GameObject buttonB;

public InventoryHotBar inventoryHotBar;

// Start is called before the first frame update
void Start()
{
  
   
}

 void OnEnable()
{
   
   itemIcon.SetActive(false);
   itemStats.SetActive(false);
   buttonA.SetActive(false);
   buttonB.SetActive(false);
}

    // Update is called once per frame
void Update()
{
   
   


}

private void Yeet()
{

    itemIcon.SetActive(true);
   
}

public void OpenInventory()
{

    gameManager.IsInventoryOpen = true;
    inventoryHotBar.GenerateHotBar();

}

public void CloseInventory()
{
   
    inventoryHotBar.ClearHotBar();
   
  
    gameManager.IsInventoryOpen = false;

    gameObject.SetActive(false);


}

public void NewSelectedItem()
{
    Yeet();
}

}**
```

Copy and paste the error, it gives a lot of helpful info to tracking down line number and such. Don’t make it harder for people to help you.

Null ref exception is the same solution every time. Look at the line number, something on it is null and it cannot run the code. Simply look at the references involved on that line, see which one is null when the error hits, then work backwards to see why it’s null.

1 Like

Sorry about that but i was trying to fix it and now it no longer throws the error but when i debug.log the variable it says null

I tried this but it seems like when the NewSelectedItem function is called it clears the variable for some unknown reason

You have your references set up incorrectly in one or more of your objects in your scene. That’s all there is to it. It’s hard for anyone on the forum to get more specific than that unless you start sharing more info like screenshots of your objects inspectors, and pasting log messages and error messages.

I figured it out I had set it to reference the prefab of the object instead of the one in the scene. I switched to referencing the object using GameObject.Find();