Object Passed and Present... But Gives Null Reference

I have a simple inventory system that updates each time a new item is equipped. It removes all the UI elements and updates the equipped view to reflect the changes using a prefab as follows…

public void UpdateEquipped()
     {
         if (Player.equipped.Length > 0)
         {
             foreach(Item i in Player.equipped)
             {
                 if(i != null)
                 {
                     GameObject icon = Instantiate(invIcon) as GameObject;
                     icon.GetComponent<InventoryIcon>().item = i;
                     icon.GetComponent<InventoryIcon>().isInventory = false;
                     icon.transform.SetParent(holder.transform);
                     icon.transform.localPosition = new Vector3(icon.transform.localPosition.x, icon.transform.localPosition.y, 0);
                     icon.transform.localScale = Vector3.one;
                 }
             }
         }
     }

When clicked, the inventory item equips itself as follows…

void EquipFromInventory()
     {
         Player.equipped[item.slotIndex] = item;
         LoadEquipped loadEquipped = GameObject.FindObjectOfType<LoadEquipped>();
         loadEquipped.ClearEquipped(false);
     }

Now the issues is that I have a tooltip that shows up when hovering over and item anywhere in the inventory. That works fine until I equip an item, then the equipped items no longer show their tooltip but instead cause a null reference error. This is how the tooltip is called…

public void OnPointerEnter(PointerEventData eventData)
     {
         if (!tooltipvisible)
         {
             Vector3 loc = gameObject.transform.position;
             if (item != null)
             {
                
                 Item i = item;
                 tooltip.ShowToolTip(i, loc);
                 tooltipvisible = true;
             }
         }
     }

and the tooltip method is…

public void ShowToolTip(Item item, Vector3 pos)
     {
         if (item != null)
         {
             Debug.Log("Showing tooltip for " + item.itemName);
             Item _item = item;
             Debug.Log(_item.itemName + " Object Found");
             nameLabel.text = _item.itemName;
             nameLabel.color = rarityColour[_item.rarity];
             slotLabel.text = _item.slot;
             staminaLabel.text = "+" + _item.stamina + " Stamina";
             strengthLabel.text = "+" + _item.strength + " Strength";
             agilityLabel.text = "+" + _item.agility + " Agility";
             intelligenceLabel.text = "+" + _item.focus + " Focus";
             if (_item.stamina == 0)
             {
                 staminaLabel.gameObject.SetActive(false);
             }
             else
             {
                 staminaLabel.gameObject.SetActive(true);
             }
             if (_item.strength == 0)
             {
                 strengthLabel.gameObject.SetActive(false);
             }
             else
             {
                 strengthLabel.gameObject.SetActive(true);
             }
             if (_item.agility == 0)
             {
                 agilityLabel.gameObject.SetActive(false);
             }
             else
             {
                 agilityLabel.gameObject.SetActive(true);
             }
             if (_item.focus == 0)
             {
                 intelligenceLabel.gameObject.SetActive(false);
             }
             else
             {
                 intelligenceLabel.gameObject.SetActive(true);
             }
             if (_item.effectText == "")
             {
                 effectLabel.gameObject.SetActive(false);
             }
             else
             {
                 effectLabel.gameObject.SetActive(true);
             }
             tooltip.SetActive(true);
             tooltip.transform.position = pos;
         }
         else
         {
             Debug.Log("No Item Passed");
         }
     }

As I said, it shows the first time around and anywhere else in the game but after equipping an item and renewing the in game icons, an error is thrown. I have debugged at various points and the item still exists right up until it needs to add the name to the nameLabel. The nameLabel object is present and assigned in the inspector and is still there during run time. The crazy thing is that the label properties to update the changes to the text component.

Thank you in advance - this is giving me a serious headache.

I think you’re focusing on the wrong variable. my bet is its not the Item. but nameLabel thats null.

I thought that too, but it works before the equipment slots are updated. It continues to work for the inventory but not the equipped slots. They are all the same prefab too. The label is still assigned in the inspector.

you provided the code but not the exception (message) - it pretty much tells you where your stuff explodes

from the code you provided you’ve debugged Item.itemName, but not nameLabel. if Item really was null then it would have nullref’d on the debug line.

nameLabel may have been set when the slots were updated, but you’re not evaluating it inside the ShowTooltip call. and the code you’ve provided doesn’t actually show us where nameLabel was working while the slots were updated. at some point you are either setting/losing the reference for the namedLabel, or showToolTip is referring to a different instance of nameLabel that was never set.

is nameLabel a field, or a property? where is it set, and what object is it set to in relation to itself?

The error is this…

“NullReferenceException: Object reference not set to an instance of an object
Tooltip.ShowToolTip (.Item item, Vector3 pos) (at Assets/Resources/Scripts/Tooltip.cs:37)
InventoryIcon.OnPointerEnter (UnityEngine.EventSystems.PointerEventData eventData) (at Assets/Resources/Scripts/InventoryIcon.cs:32)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerEnterHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:24)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerEnterHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)
UnityEngine.EventSystems.EventSystem:Update()”

It points to the line…

nameLabel.text = _item.itemName;

For some reason, the connection with the Text component nameLabel is being lost. It is, however, still visible in the hierarchy and still set in the inspector.

When you equip you remove all UI, does that mean you destroy the text component? If not then what does the code for this line do?

loadEquipped.ClearEquipped(false);
public void ClearEquipped(bool doesUpdate)
    {
        foreach (Transform child in holder.transform)
        {
            child.GetComponent<InventoryIcon>().DestroySelf();
        }

        if (doesUpdate)
        {
            UpdateEquipped();
        }
    }

…and the DestroySelf() method is simply…

public void DestroySelf()
    {
        Destroy(gameObject);
    }