Post by kingmob on Nov 5, 2019 9:16:51 GMT
Keep getting an dictionary error even though as far as functionality goes the script is doing what I need it to do. Didn't know if you had seen this error before, and I can't find much information as to what these dictionary errors are.
KeyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) (at <df7127ba07dc446d9f5831a0ec7b1d63>:0)
DarkTonic.CoreGameKit.PoolBoss.Spawn (System.String itemName, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation, UnityEngine.Transform parentTransform) (at Assets/Plugins/DarkTonic/CoreGameKit/Scripts/PoolBoss/PoolBoss.cs:775)
VRWeaponInteractor.InventoryStorage.Update () (at Assets/VRWeaponInteractor/Scripts/Inventory/InventoryStorage.cs:72)
//using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRInteraction;
using DarkTonic;
using DarkTonic.CoreGameKit;
namespace VRWeaponInteractor
{
public class InventoryStorage : MonoBehaviour
{
public GameObject prefab;
public string prefabString;
public bool infinte;
public int storageCapacity = 10;
public int initialStoredItems;
public GameObject itemObject;
public Transform itemObjectTransform;
public Vector3 spawnPosition;
public Quaternion spawnQuaternion;
public Transform itemTransformParent;
private int _storedItems;
private ItemSlot _itemSlot;
private Queue<GameObject> _disabledStoredItems = new Queue<GameObject>();
private string acceptedItemId;
public int storedItems
{
get { return _storedItems; }
set { _storedItems = value; }
}
void Start()
{
_itemSlot = GetComponent<ItemSlot>();
_storedItems = initialStoredItems;
if (_itemSlot == null)
{
Debug.LogError("Inventory Storage requires an item slot: " + name, gameObject);
return;
}
if (prefab == null)
{
Debug.LogError("No prefab on Inventory Storage: " + name, gameObject);
return;
}
VRInteractableItem item = prefab.GetComponentInChildren<VRInteractableItem>();
if (item == null)
{
Debug.LogError("No VRInteractbleItem on prefab object: " + name, gameObject);
return;
}
if (_itemSlot.GetSlotPosition(item) == null)
{
Debug.LogError("Prefab in Inventory Storage doesn't have an inventory slot accepted id: " + name, gameObject);
return;
}
acceptedItemId = item.itemId;
}
void Update()
{
if ((!infinte && storedItems == 0) || _itemSlot.hasItem) return;
if (!infinte) storedItems--;
GameObject itemObject;
if (_disabledStoredItems.Count == 0)
{
itemObjectTransform = DarkTonic.CoreGameKit.PoolBoss.Spawn(prefabString,spawnPosition,spawnQuaternion,itemTransformParent);
itemObject = itemObjectTransform.gameObject;
}
else itemObject = _disabledStoredItems.Dequeue();
VRInteractableItem item = itemObject.GetComponentInChildren<VRInteractableItem>();
_itemSlot.AddItemToSlot(item);
}
public bool TryAddItem(VRInteractableItem item)
{
if (item.itemId != acceptedItemId) return false;
if ((infinte || storedItems < storageCapacity) && !_disabledStoredItems.Contains(item.item.gameObject))
{
//Can store
if (!infinte) storedItems++;
item.item.gameObject.SetActive(false);
_disabledStoredItems.Enqueue(item.item.gameObject);
return true;
}
return false;
}
}
}
Thanks for the help, and apologies in advance to what is probably another dumb error on my part.