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.