Implement stacking to my inventory system in Unity - c#

I'm currently working on a game, and now I've just finished the inventory system, which is highly inspired by the one Brackeys made a while back. Right now the player can pick up items, which will go into the inventory's list and be displayed on some UI. After having finished this, I tried to make items stack together, however I was never able to find a working solution. I don't want anything fancy, I just want every item to be able to stack infinitely. If anyone could help me out I'd greatly appreciate it!
Sorry for putting so much code in here, I'm not sure what I should and shouldn't add.
My "Items" script:
using UnityEngine;
[CreateAssetMenu(fileName = "New item", menuName = "Inventory/Items")]
public class Item : ScriptableObject
{
new public string name = "New item";
public Sprite icon = null;
public bool itemDefaut = false;
public virtual void Use()
{
Debug.Log("Using " + name);
}
}
My "Inventory" script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
#region Singleton
public static Inventory instance;
void Awake()
{
if (instance != null)
{
Debug.LogWarning("More than one inventory instance found!");
return;
}
instance = this;
}
#endregion
public delegate void OnItemChanged();
public OnItemChanged onItemChangedCallback;
public List<Item> items = new List<Item>();
public void Add(Item item)
{
items.Add(item);
if (onItemChangedCallback != null)
{
onItemChangedCallback.Invoke();
}
}
public void Remove(Item item)
{
items.Remove(item);
if (onItemChangedCallback != null)
{
onItemChangedCallback.Invoke();
}
}
}
My "InventorySlot" script:
using UnityEngine;
using UnityEngine.UI;
public class InventorySlot : MonoBehaviour
{
Item item;
public Image icon;
public void AddItem(Item newItem)
{
item = newItem;
icon.sprite = item.icon;
icon.enabled = true;
}
public void RemoveItem()
{
item = null;
icon.sprite = null;
icon.enabled = false;
}
public void UseItem()
{
if (item != null)
{
item.Use();
}
}
}
My "InventoryUI" script:
using UnityEngine;
public class InventoryUI : MonoBehaviour
{
public Transform itemsParent;
public GameObject inventoryUI;
Inventory inventory;
InventorySlot[] slots;
// Start is called before the first frame update
void Start()
{
inventory = Inventory.instance;
inventory.onItemChangedCallback += UpdateUI;
slots = itemsParent.GetComponentsInChildren<InventorySlot>();
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Inventory"))
{
inventoryUI.SetActive(!inventoryUI.activeSelf);
}
if (inventoryUI.activeSelf)
{
Time.timeScale = 0;
}
else
{
Time.timeScale = 1;
}
}
void UpdateUI()
{
for (int i = 0; i < slots.Length; i++)
{
if (i < inventory.items.Count)
{
slots[i].AddItem(inventory.items[i]);
}
else
{
slots[i].RemoveItem();
}
}
}
}
My "ItemPickup" script:
using UnityEngine;
public class ItemPickup : Interactable
{
public Item item;
public override void Interact()
{
base.Interact();
PickUp();
}
void PickUp()
{
Debug.Log("Picking up " + item.name);
Inventory.instance.Add(item);
Destroy(gameObject);
}
}
And my "Interactable" script:
using UnityEngine;
public class Interactable : MonoBehaviour
{
public float radius = 3f;
[SerializeField] Player player;
bool interacted = false;
void Start()
{
interacted = false;
}
public virtual void Interact()
{
//This method is meant to be overwritten
Debug.Log("Interacting with " + transform.name);
}
void Update()
{
if (!interacted)
{
float distance = Vector3.Distance(player.transform.position, transform.position);
if (distance <= radius)
{
Interact();
interacted = true;
}
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, radius);
}
}
Finally, here's what my hierarchy looks like for my inventory (just replace "inventaire" by "inventory" and don't mind the item names):

If I understand you correctly it sounds like you basically want a counter for the slot so you can add multiple of the Item instance.
You could probably do something like
public class InventorySlot : MonoBehaviour
{
// Have a counter for the amount of items added
int _amount;
Item item;
public Image icon;
public Text amountText;
// optionally add an amount parameter
// if not passed 1 is used but allows to add multiple at once
public void AddItem(Item newItem, int amount = 1)
{
// If there is no item yet
// then simply add the first one
if(!item)
{
item = newItem;
icon.sprite = item.icon;
icon.enabled = true;
_amount = amount;
}
else
{
// Otherwise check first if it is the same item
if(item != newItem)
{
Debug.LogWarning($"Type mismatch between current item {item} and added item {newItem}!", this);
// TODO handle this?
return;
}
// simply increase the amount
_amount += amount;
}
// update text
amountText.text = _amount.ToString();
}
// Again maybe add optional parameter
// if nothing is passed 1 is used
// but allows to remove multiple at once
public void RemoveItem(int amount = 1)
{
// check if enough
if(!item)
{
Debug.LogWarning("This slot is empty! Can't remove", this);
// TODO handle this?
return;
}
if(_amount - amount < 0)
{
Debug.LogWarning($"Not enough items in this slot to remove {amount}!", this);
}
// simply reduce the amount
_amount -= amount;
// update the text
_amountText.text = amount.ToString();
// Only if you reached 0 -> removed the last item
// reset this slot
if(amount == 0)
{
item = null;
icon.sprite = null;
icon.enabled = false;
}
}
public void UseItem()
{
if (item)
{
item.Use();
// TODO also remove one?
}
}
}

Related

Hello, how could i improve this inventory system to be able to swap items, stacking items on dragging?

I'm really just a beginner and trying to learn game development, but i can't seem to figure out the logic behind this.. if the inventory has already an item inside and if the current object can't be stacked on top of it, then swap out the 2 object
the script i'm trying to implement it:
public void OnDrop(PointerEventData eventData)
{
//on drop if the inventory slot doesn't have child object then we know it's a free slot, so we can drag there the grabbed item.
if(transform.childCount == 0)
{
GameObject dropped = eventData.pointerDrag;
InventoryItem inventoryItem = dropped.GetComponent<InventoryItem>();
inventoryItem.parentAfterDrag = transform;
}
else
{
//on drop if the inventory has already an item inside and if the current object can't be stacked on top of it, then swap out the 2 object
//but how do i know what is the object that is inside this slot?
}
}
}
here is the InventoryManager script:
public class InventoryManager : MonoBehaviour
{
public int maxStackedItems = 4;
int selectedSlot = -1;
public InventorySlot[] inventorySlots;
public GameObject inventoryItemPrefab;
public GameObject mainInventory;
public bool isMainInventoryOpened;
public bool AddItem(Item item)
{
//checks if there is any slot with the same kind of item as this, which isnt in max stack
for (int i = 0; i < inventorySlots.Length; i++)
{
InventorySlot slot = inventorySlots[i];
InventoryItem itemInSlot = slot.GetComponentInChildren<InventoryItem>();
if (itemInSlot != null && itemInSlot.item == item && itemInSlot.count < maxStackedItems && itemInSlot.item.stackable == true)
{
itemInSlot.count++;
itemInSlot.RefreshCount();
return true;
}
}
//looks for an empty slot
for (int i = 0; i < inventorySlots.Length; i++)
{
InventorySlot slot = inventorySlots[i];
InventoryItem itemInSlot = slot.GetComponentInChildren<InventoryItem>();
if(itemInSlot == null)
{
SpawnNewItem(item, slot);
return true;
}
}
return false;
}
void SpawnNewItem(Item item, InventorySlot slot)
{
GameObject newItemGo = Instantiate(inventoryItemPrefab, slot.transform);
InventoryItem inventoryItem = newItemGo.GetComponent<InventoryItem>();
inventoryItem.InitializeItem(item);
}
public Item GetSelectedItem(bool use)
{
InventorySlot slot = inventorySlots[selectedSlot];
InventoryItem itemInSlot = slot.GetComponentInChildren<InventoryItem>();
if (itemInSlot != null)
{
Item item = itemInSlot.item;
if(use == true)
{
itemInSlot.count--;
if(itemInSlot.count <= 0)
{
Destroy(itemInSlot.gameObject);
}
else
{
itemInSlot.RefreshCount();
}
}
return item;
}
return null;
}
}
and the InventoryItem script:
public class InventoryItem : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
[HideInInspector] public Transform parentAfterDrag;
[HideInInspector] public Item item;
[HideInInspector] public int count = 1;
public Image image;
public TMP_Text countText;
public void InitializeItem (Item newItem)
{
item = newItem;
image.sprite = newItem.image;
RefreshCount();
}
public void RefreshCount()
{
countText.text = count.ToString();
bool textActive = count > 1;
countText.gameObject.SetActive(textActive);
}
public void OnBeginDrag(PointerEventData eventData)
{
parentAfterDrag = transform.parent;
transform.SetParent(transform.root);
transform.SetAsLastSibling();
image.raycastTarget = false;
}
public void OnDrag(PointerEventData eventData)
{
transform.position = Input.mousePosition;
}
public void OnEndDrag(PointerEventData eventData)
{
transform.SetParent(parentAfterDrag);
image.raycastTarget = true;
}
}
i've tried to figure out what is the item on that occupied slot but i don't seem to understand this logic yet.

UI element keeps upscaling itself upon game start

Pretty new to developing so please excuse any obvious errors.
I'm currently trying to build an inventory system for my game but the item slots keep upscaling themselves upon game start.
The prefab starts with a scaling of: x=1, y=1, z=1;
On game start this inflates to: x=53.1, y=53.1, z=53.1;
Here is the scale of the prefab:HERE
And here is the scale of the prefab upon game start: HERE
Here is the script to update the items:
using UnityEngine;
using UnityEngine.UI;
public class UIItem : MonoBehaviour
{
public Item item;
private Image spriteImage;
private void Awake()
{
spriteImage = GetComponent<Image>();
UpdateItem(null);
}
public void UpdateItem(Item item)
{
this.item = item;
if (this.item != null)
{
spriteImage.color = Color.white;
spriteImage.sprite = this.item.icon;
}
else
{
spriteImage.color = Color.clear;
}
}
}
And here is the script used to update the slots and to add the items to the inventory:
using System.Collections.Generic;
using UnityEngine;
public class UIInventory : MonoBehaviour
{
public List<UIItem> uIItems = new List<UIItem>();
public GameObject slotPrefab;
public Transform slotPanel;
public int numberOfSlots = 16;
private void Awake()
{
for (int i = 0; i < numberOfSlots; i++)
{
GameObject instance = Instantiate(slotPrefab);
instance.transform.SetParent(slotPanel);
uIItems.Add(instance.GetComponentInChildren<UIItem>());
}
}
public void UpdateSlot(int slot, Item item)
{
uIItems[slot].UpdateItem(item);
}
public void AddNewItem(Item item)
{
UpdateSlot(uIItems.FindIndex(i => i.item == null), item);
}
public void RemoveItem(Item item)
{
UpdateSlot(uIItems.FindIndex(i => i.item == item), null);
}
}
Here is the inventory script itself:
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
public List<Item> characterItems = new List<Item>();
public ItemDatabase itemDatabase;
public UIInventory inventoryUI;
private void Start()
{
GiveItem(1);
GiveItem("Clue 5");
RemoveItem(1);
}
public void GiveItem(int id)
{
Item itemToAdd = itemDatabase.GetItem(id);
characterItems.Add(itemToAdd);
inventoryUI.AddNewItem(itemToAdd);
Debug.Log("Added item:" + itemToAdd.name);
}
public void GiveItem(string itemName)
{
Item itemToAdd = itemDatabase.GetItem(itemName);
characterItems.Add(itemToAdd);
inventoryUI.AddNewItem(itemToAdd);
Debug.Log("Added item:" + itemToAdd.name);
}
public Item CheckForItem(int id)
{
return characterItems.Find(item => item.id == id);
}
public void RemoveItem(int id)
{
Item item = CheckForItem(id);
if (item != null)
{
characterItems.Remove(item);
inventoryUI.RemoveItem(item);
Debug.Log("Item removed:" + item.name);
}
}
}
I've tinkered on this for the past four hours and have nothing that seems to work. I've tried the Aspect Ratio Fitter and it only made it more. I can try to explain further if needed.
If anyone could help, it would be greatly appreciated.

Avoid multiple equip item

In the InventoryUIDetails class, in the SetItem function, we are adding a listener to the itemInteractButton's onClick event.
Every time I select an item in the inventory, it will add an additional listener. This means that clicking log potion multiple times and then clicking 'Drink' (button) will drink multiple log potions.
Where should I call the RemoveAllListeners() to ensure it is the only function that is called when clicking the interact button.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InventoryUIDetails : MonoBehaviour {
Item item;
Button selectedItemButton, itemInteractButton;
Text itemNameText, itemDescriptionText, itemInteractButtonText;
public Text statText;
void Start() {
itemNameText = transform.Find ("Item_Name").GetComponent<Text> ();
itemDescriptionText = transform.Find ("Item_Description").GetComponent<Text> ();
itemInteractButton = transform.GetComponentInChildren<Button> ();
itemInteractButtonText = itemInteractButton.GetComponentInChildren<Text>();
gameObject.SetActive (false);
}
public void SetItem(Item item, Button selectedButton) {
gameObject.SetActive (true);
statText.text = "";
if (item.Stats != null) {
foreach(BaseStat stat in item.Stats) {
statText.text += stat.StatName + ": " + stat.BaseValue + "\n";
}
}
itemInteractButton.onClick.RemoveAllListeners ();
this.item = item;
selectedItemButton = selectedButton;
itemNameText.text = item.ItemName;
itemDescriptionText.text = item.Description;
itemInteractButtonText.text = item.ActionName;
itemInteractButton.onClick.AddListener (OnItemInteract);
}
public void OnItemInteract() {
if (item.ItemType == Item.ItemTypes.Consumable) {
InventoryController.Instance.ConsumeItem (item);
Destroy (selectedItemButton.gameObject);
} else if (item.ItemType == Item.ItemTypes.Weapon) {
InventoryController.Instance.EquipItem (item);
Destroy (selectedItemButton.gameObject);
}
item = null;
gameObject.SetActive (false);
}
}
On this class, SetItem called.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InventoryController : MonoBehaviour {
public static InventoryController Instance { get; set; }
public PlayerWeaponController playerWeaponController;
public ConsumableController consumableController;
public InventoryUIDetails inventoryDetailsPanel;
public List<Item> playerItems = new List<Item> ();
void Start() {
if (Instance != null && Instance != this)
Destroy (gameObject);
else
Instance = this;
playerWeaponController = GetComponent<PlayerWeaponController> ();
consumableController = GetComponent<ConsumableController> ();
GiveItem ("Kampilan");
GiveItem ("potion_log");
}
public void GiveItem(string itemSlug) {
Item item = ItemDatabase.Instance.GetItem (itemSlug);
playerItems.Add(item);
Debug.Log (playerItems.Count + " items in inventory. Added: " + itemSlug);
UIEventHandler.ItemAddedToInventory (item);
}
public void SetItemDetails(Item item, Button selectedButton) {
inventoryDetailsPanel.SetItem (item, selectedButton);
}
public void EquipItem(Item itemToEquip) {
playerWeaponController.EquipWeapon (itemToEquip);
}
public void ConsumeItem(Item itemToConsume) {
consumableController.ConsumeItem (itemToConsume);
}
}
Consider add the listener only once
public class InventoryUIDetails : MonoBehaviour {
void Start() {
...
itemInteractButton.onClick.AddListener (OnItemInteract);
}
void OnDestroy() {
itemInteractButton.onClick.RemoveListener (OnItemInteract);
}
public void OnItemInteract() {
if (item == null)
return;
...
}
}

Interactable button UNITY

I'm trying to make a simple condition:
If the value I have, is less than the price it costs the item, the button is disabled.
If the value I have, is greater than or equal to the price it costs the item the button is enabled and I can buy it.
But when I test, I have some problems.
First, if I have less than the item cost the button is enabled, and only when I click on it it is when it disables.
Second, if I have less than the item cost and I click on it it disables, but if I get enough to purchase the item, the button is not enabled again.
How do I to be checked these variables all the time? If I have enough the button is enabled if you do not have it disables.
Bellow my scrip:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class BuySkin : MonoBehaviour {
public int price;
public Button buyBee1;
void OnEnable ()
{
//Register Button Events
buyBee1.onClick.AddListener (() => buySkin (buyBee1));
}
public void buySkin(Button button)
{
if (BeeCoinScore.coin >= price) {
BeeCoinScore.coin -= price;
buyBee1.interactable = false;
}
if (BeeCoinScore.coin < price) {
buyBee1.interactable = false;
}
}
void OnDisable ()
{
//Un-Register Button Events
buyBee1.onClick.RemoveAllListeners ();
}
}
Try this out with some prefabs!
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class GameController : MonoBehaviour
{
public int coins;
private int spherePrice = 100, cubePrice = 50;
public GameObject player;
public GameObject[] availablePrefabs;
public List<GameObject> mySkins;
public Button btnSphere, btnCube;
public Text txtSphere, txtCube;
void Start ()
{
string serializedMySkins = PlayerPrefs.GetString ("skins", "");
string serializedPlayer = PlayerPrefs.GetString ("player", "");
// skins desserialization
if (serializedMySkins == "")
mySkins = new List<GameObject> ();
else {
var a = serializedMySkins.Split (',');
for (int i = 0; i < a.Length; i++) {
if (a [i] == "Sphere") {
mySkins.Add (availablePrefabs [0]);
}
if (a [i] == "Cube") {
mySkins.Add (availablePrefabs [1]);
}
}
}
// player desserialization
if (serializedPlayer != "") {
if (serializedPlayer == "Sphere") {
player = availablePrefabs [0];
}
if (serializedPlayer == "Cube") {
player = availablePrefabs [1];
}
} else {
player = mySkins [0];
}
coins = PlayerPrefs.GetInt ("coins", 0);
coins = 1000;
}
void Update ()
{
if (mySkins.Contains (availablePrefabs [0])) {
txtSphere.text = "Usar esfera";
} else {
btnSphere.interactable = coins >= spherePrice;
}
if (mySkins.Contains (availablePrefabs [1])) {
txtCube.text = "Usar cubo";
} else {
btnCube.interactable = coins >= cubePrice;
}
}
public void play ()
{
player = (GameObject)Instantiate (player, new Vector2 (0, 0), Quaternion.identity);
}
public void verifySkin (GameObject skinPrefab)
{
if (mySkins.Contains (skinPrefab)) {
useSkin (skinPrefab);
} else if (coins >= priceOf (skinPrefab)) {
buySkin (skinPrefab, priceOf (skinPrefab));
}
}
public void buySkin (GameObject skinPrefab, int price)
{
mySkins.Add (skinPrefab);
coins -= price;
string skinsHash = "";
for (int i = 0; i < mySkins.Count; i++) {
skinsHash += mySkins [i].name + ",";
}
Debug.Log (skinsHash);
PlayerPrefs.SetInt ("coins", coins);
PlayerPrefs.SetString ("skins", skinsHash);
PlayerPrefs.Save ();
}
public void useSkin (GameObject skinPrefab)
{
player = skinPrefab;
PlayerPrefs.SetString ("player", player.name);
PlayerPrefs.Save ();
}
private int priceOf (GameObject skinPrefab)
{
if (skinPrefab == availablePrefabs [0])
return spherePrice;
else if (skinPrefab == availablePrefabs [1])
return cubePrice;
else
return 0;
}
}
OnEnable()is called when the object becomes enabled and active.
you need Update() as it is getting called every frame it will check whether your value is less than or greater than price of item.You may also try like this.
// I think that you are making an buymenu, so you can disable and enable your menu with ui button and check money you have
using System.Collections;
using UnityEngine.UI;
public class BuySkin : MonoBehaviour
{
public int price;
public static int money;// money you have
public Button thisbuyBee1;
public void buychkr()
{
if(price>= money)
{
thisbuyBee1.interactable = false;
}
else
{
thisbuyBee1.interactable = true;
}
}
void Update()
{
buychkr();
}
}

Shuffling Items in Unity 3D

I have this script of an array that shows a listing of items.
Now the thing is I only want this list to have five items shown out of ten and also shuffled, so you can't have the same list every time you start a new game
I was thinking if there should be a Random.Range implemented but I don't know where.
Please Help, and explain what should be done. I'm still a bit new to this and Thanks.
Here's the script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class RayCasting : MonoBehaviour
{
public float pickupDistance;
public List<Item> items;
#region Unity
void Start ()
{
Screen.lockCursor = true;
}
void Update ()
{
RaycastHit hit;
Ray ray = new Ray(transform.position, transform.forward);
if (Physics.Raycast(ray, out hit, pickupDistance))
{
foreach(Item item in items)
{
if(Input.GetMouseButtonDown(0))
{
if (item.gameObject.Equals(hit.collider.gameObject))
{
numItemsCollected++;
item.Collect();
break;
}
}
}
}
}
void OnGUI()
{
GUILayout.BeginArea(new Rect(130,400,100,100));
{
GUILayout.BeginVertical();
{
if (numItemsCollected < items.Count)
{
foreach (Item item in items)
{
GUILayout.Label(string.Format("[{0}] {1}", item.Collected ? "" + item.password: " ", item.name ));
}
}
else
{
GUILayout.Label("You Win!");
}
}
GUILayout.EndVertical();
}
GUILayout.EndArea();
}
#endregion
#region Private
private int numItemsCollected;
#endregion
}
[System.Serializable]
public class Item
{
public string name;
public GameObject gameObject;
public int password;
public bool Collected { get; private set; }
public void Collect()
{
Collected = true;
gameObject.SetActive(false);
}
public void passwordNumber()
{
password = 0;
Collected = true;
gameObject.SetActive(false);
}
}
I assume you'll want to leave items intact without removing any Items, so I'd suggest creating a second List called finalItems, which will contain your 5 random Items.
public List<Item> items;
public List<Item> finalItems;
#region Unity
void Start ()
{
Screen.lockCursor = true;
// Do a while loop until finalItems contains 5 Items
while (finalItems.Count < 5) {
Item newItem = items[Random.Range(0, items.Count)];
if (!finalItems.Contains(newItem)) {
finalItems.Add(newItem);
}
}
}
And then in your foreach statements, loop through finalItems instead of items.
This will give you 5 random Items every game!

Categories