- Home /
Inventory System-Bag System-Static List-Changing values from other scripts-2020
Hey guys So im currently in the process of creating an inventory system. how this works, Is I pick up a bag, once the bag is picked up, it create an empty inventory of "List" within the bag script.
Now what im trying to do, is make the bag you picked up your inventory, rather than the inventory being on the player by default. I am also wanting each item that was pickedup with said bag, to store/save those items if the bag were to be dropped. SO on and so forth.
Now that part ill come to later, right now Im stuck on changing the values from a different script. Now when the "AddItem" method from within the "Inventory" class is hit, it works perfectly fine WHEN WITHIN the inventory class.. However, when I try to add an item through this same method, from a different script, it simply doesnt work...
For example: I pick up bag, new inventory list created within the bag/inventory script. I then walk over to an ammo clip. When I pick this ammo clip up, I within my Ammo clip script, check some edge cases to make sure that the player has bag, player within colliders, etc. But once you Hit E, it should add the ammo clip info into this list within the bag Inventory.
Now Ive had success when doing things like this, It's usually worked with turning that variable into a static, so that I can grab and manipulate it from anywhere else. But this time around, its not liking me turning this list into a static? I keep getting different errors, and it WILL NOT allow me to call and add an item from the "AddItem" Method, from my ammo clip script? ANy help Is appreciated, thanks.
Item class:
public class Item
{
public enum ItemType {
Glock_Handgun,
Money,
Pistol_Clip,
Rifle_Clip,
Sniper_Clip,
Shotgun_Shells
}
public ItemType itemType;
public int amount;
}
Inventory Class:
public class Inventory {
public static List<Item> itemList;
public Inventory() {
itemList = new List<Item>();
// WHen adding items here, works perfectly fine- but I want to be able to cal these methods from different script depending on which item is being picked up..
// AddItem(new Item {
// itemType = Item.ItemType.Glock_Handgun, amount = 1
// });
Debug.Log("Inventory INSIDE OBJECT: amt: "+ itemList.Count);
}
public void AddItem(Item item)
itemList.Add(item);
}
}
Ammo Clip Class:
public class PistolColtAmmo : MonoBehaviour
{
// public int bagAmmo;
private static int bagAmmo;
public int ammoAmt;
[SerializeField] public int fullClip;
[SerializeField]
private Inventory inventory;
private static List<Item> itemList;
// public static List<Item> itemList = new List<Item>();
[SerializeField] AudioSource ammoPickup;
public void OnTriggerStay(Collider other)
{
// check if its the player:
if(other.tag == "Player")
{
// Grab global bag variable:
bagAmmo = HandgunScriptLPFP.bagAmmo;
// Check to see if user hits the "E" key:
if(Input.GetKeyDown(KeyCode.E))
{
//TEstting grounds for inventory adding:--------------
var inv = Inventory.itemList;
Debug.Log("TEST HERE INV INV INV: "+ inv.Count);
Inventory.AddItem(new Item {
itemType = Item.ItemType.Glock_Handgun, amount = 3
});
Debug.Log("TEST HERE INV INV AFTER!!: "+ Inventory.itemList.Count);
}}
PS there more to the code for ammo clip, but Ive shown the relevant parts.