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();
}
}**
```