- Home /
Unity 3d Inventory Stacking Items [C#]
So I was doing an Inventory for my RPG game, but now I´m stuck on an Problem: I want to Stack my Items in that Inventory but I don´t know how to do this.
this is my Inventory Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
public GameObject Player;
public GameObject InventoryPanel;
public GameObject Overlay;
public GameObject statsoverlay;
public static Inventory instance;
public List<Item> list = new List<Item>();
void UpdatePanelSlot()
{
int Index = 0;
foreach(Transform child in InventoryPanel.transform)
{
ItemSlotController slot = child.GetComponent<ItemSlotController>();
if(Index < list.Count)
{
slot.item = list[Index];
}
else
{
slot.item = null;
}
slot.UpdateInfo();
Index++;
}
}
private void Start()
{
instance = this;
UpdatePanelSlot();
}
public void Add(Item item)
{
if(list.Count < 98)
{
list.Add(item);
}
UpdatePanelSlot();
}
public void Remove(Item item)
{
list.Remove(item);
UpdatePanelSlot();
}
}
this is my Item Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Item : ScriptableObject
{
public string ItemName;
public Sprite Icon;
public string description;
public virtual void use()
{
}
}
but the Item Script is just the base for other versions of Items just like tools or eatables and stuff. and the last Script is the Slot Controller wich controlls my Inventory Slots (like the name said):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ItemSlotController : MonoBehaviour
{
public Item item;
public GameObject Ausklappen;
private void Start()
{
UpdateInfo();
}
public void UpdateInfo()
{
Image displayimage = transform.Find("Image").GetComponent<Image>();
if (item)
{
displayimage.sprite = item.Icon;
displayimage.color = Color.white;
}
else
{
displayimage.sprite = null;
displayimage.color = Color.clear;
}
}
public void use()
{
if (item)
{
item.use();
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Camera Zoom not working 1 Answer
Error CS0029 1 Answer
Rpg creator kit doesnt work? 1 Answer
UI RPG Dialog Sytem 0 Answers
Going from world to interiors of buildings in simple 3D RPG Game: teleport or new scene? 1 Answer