Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Stacking objects of the same type [inventory system issues]

Discussion in 'Scripting' started by PhantomProgramming, Apr 16, 2020.

  1. PhantomProgramming

    PhantomProgramming

    Joined:
    Jan 16, 2019
    Posts:
    60
    Hey there, I'm attempting to create an inventory system that allows the player to pickup and drop items. I followed a tutorial from BlackThornProd (
    ) and got the inventory to pretty much work. My issue is getting items of the same type to stack.

    As I have it, I can set a max stack size and pickup objects, once the objects in a slot exceed the max stack size, the next slot begins to fill. My issue is the script doesn't know how to tell one type of object from another (example: Leather and Paper stack together). I'm trying to sort objects to the correct slot based on type.

    Here's my script that fills the slots this is attached to any object that gets picked up:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Pickup : MonoBehaviour
    6. {
    7.  
    8.     private Inventory inventory;
    9.     public GameObject itemButton;
    10.  
    11.     private void Start()
    12.     {
    13.         inventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
    14.     }
    15.  
    16.     private void OnTriggerEnter2D(Collider2D other)
    17.     {
    18.         if (other.CompareTag("Player"))
    19.         {
    20.             for (int i = 0; i < inventory.slots.Length; i++)
    21.             {
    22.                 if(inventory.isFull[i] == false)
    23.                 {
    24.                     if (inventory.slots.Length > inventory.maxStack) {
    25.                         inventory.isFull[i] = true;
    26.                     }
    27.                    
    28.                     Instantiate(itemButton, inventory.slots[i].transform, false);
    29.                     Destroy(gameObject);
    30.                     break;
    31.  
    32.                 }
    33.             }
    34.         }
    35.     }
    36.  
    37.  
    38. }
    39.  
    Here is the actual slot code itself

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Slot : MonoBehaviour
    6. {
    7.     private Inventory inventory;
    8.     public int i;
    9.  
    10.     private void Start()
    11.     {
    12.         inventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
    13.  
    14.     }
    15.     private void Update()
    16.     {
    17.         if(transform.childCount <= 0)
    18.         {
    19.             inventory.isFull[i] = false;
    20.         }
    21.     }
    22.  
    23.     public void DropItem()
    24.     {
    25.         foreach (Transform child in transform)
    26.         {
    27.             child.GetComponent<Spawn>().SpawnDroppedItem();
    28.             GameObject.Destroy(child.gameObject);
    29.         }
    30.     }
    31.  
    32. }
    33.  
    Any tips are greatly appreciated!
     
unityunity