Hi!! I’m making a simple dungeon crawler for mobile but I’m not experienced with C#. So, I just want to display the item in the player’s inventory when he collides with items. I’m using a dedicated item class that holds the info of that item and an inventory class that accesses the item class list, etc.
This is the Item Class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Items : MonoBehaviour
{
//List
public static List<Items> listItem = new List<Items>();
public string name;
public string type;
//Constructor
public Items(string n, string t)
{
this.name = n;
this.type = t;
}
//Default Constructor
public Items()
{
}
}
Inventory Class:
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour
{
public Text title;
public Text countItem;
Items item;
void Start()
{
item = new Items();
}
private void OnTriggerEnter2D(Collider2D collision)
{
// Checking for the item collision
if (collision.gameObject.tag == "item")
{
Items currItem = collision.gameObject.GetComponent<Items>();
//getting the data of the item collided
item.name = currItem.name;
item.type = currItem.type;
//storing in string for checking the condition
string name = item.name;
string type = item.type;
//Adding the item collided
Items.listItem.Add(item);
//Displaying the title of item in text
title.text = item.name;
// Displaying the current stock of that single item in text (which isn't working)
countItem.text = (Items.listItem.Count(x => x.name == name)).ToString();
}
}
}
This is the problem here, instead of displaying just the “Axe” count, it’s displaying the whole list!
Please help me.