Search Unity

need help with inventory system

Discussion in 'Scripting' started by stakensj, Aug 25, 2021.

  1. stakensj

    stakensj

    Joined:
    Oct 23, 2020
    Posts:
    35
    So basically I am trying to implement an inventory system to my game, by following this tutorial

    But when I get to the part where you add items into the inventory there is some kind of error that I don't understand
    here are the scripts that it mentions:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [CreateAssetMenu(fileName = "New Inventory", menuName = "Inventory System/Inventory")]
    6. public class invnetoryObjects : ScriptableObject
    7. {
    8.     public List<InventorySlot> Container = new List<InventorySlot>();
    9.     public void AddItem(ItemObject _item, int _amount)
    10.     {
    11.         bool hasItem = false;
    12.         for (int i = 0; 1 < Container.Count; i++)
    13.         {
    14.             if(Container[i].item == _item)
    15.             {
    16.                 Container[i].AddAmount(_amount);
    17.                 hasItem = true;
    18.                 break;
    19.             }
    20.         }
    21.         if (!hasItem)
    22.         {
    23.             Container.Add(new InventorySlot(_item, _amount));
    24.         }
    25.     }
    26. }
    27. [System.Serializable]
    28. public class InventorySlot
    29. {
    30.     public ItemObject item;
    31.     public int amount;
    32.     public InventorySlot(ItemObject _item, int _amount)
    33.     {
    34.         item = _item;
    35.         amount = _amount;
    36.     }
    37.     public void AddAmount(int value)
    38.     {
    39.         amount += value;
    40.     }
    41. }
    and

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Enteractable : MonoBehaviour
    6. {
    7.     public ItemObject item;
    8.     public int Amount = 20;
    9.     public float helth = 3f;
    10.     public bool DestroyAfterUse = false;
    11.  
    12.     public GameObject Player;
    13.     public invnetoryObjects Pinventory;
    14.  
    15.      void Update()
    16.      {
    17.         if (helth <= 0)
    18.         {
    19.  
    20.             Pinventory.AddItem(item, Amount);
    21.             if (DestroyAfterUse)
    22.             {
    23.             Destroy(gameObject);
    24.             }
    25.         }
    26.      }
    27. }
    28.  
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    The exit conditional on your for loop has a typo, you put a 1 where you meant an i.
     
    exiguous likes this.
  3. stakensj

    stakensj

    Joined:
    Oct 23, 2020
    Posts:
    35
    thx it worked