so i followed a tutorial on creating an inventory system using scriptable objects and i want now want to create weapons that the player can pick up and use and be stored in that inventory systema nd im lost on how to do that please anyone help, ill attach some of the scripts i used
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu( menuName = "Iventory Item Data")]
public class InventoryItemData : ScriptableObject
{
public string id;
public string displayName;
public Sprite icon;
public GameObject prefab;
public ItemType itemType;
public int damage;
public float attackSpeed;
public enum ItemType
{
weapon,
armor,
consumable,
other
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InventorySystem: MonoBehaviour
{
private static InventorySystem _instance;
public static InventorySystem Instance
{
get
{
if (_instance == null)
{
_instance = FindObjectOfType<InventorySystem>();
if (_instance == null)
{
Debug.LogError("An InventorySystem instance wasn't found in the scene");
}
}
return _instance;
}
}
public Dictionary<InventoryItemData, InventoryItem> m_itemDictionary;
public List<InventoryItem> inventory { get; private set; }
public event Action onInventoryChangedEvent; // Event for inventory change updates
private void Awake()
{
inventory = new List<InventoryItem>();
m_itemDictionary = new Dictionary<InventoryItemData, InventoryItem>();
}
public void Add (InventoryItemData referenceData)
{
if(m_itemDictionary.TryGetValue(referenceData, out InventoryItem value))
{
value.AddToStack();
}
else
{
InventoryItem newItem = new InventoryItem(referenceData);
inventory.Add(newItem);
m_itemDictionary.Add(referenceData, newItem);
}
onInventoryChangedEvent.Invoke();
}
public void Remove (InventoryItemData referenceData)
{
if (m_itemDictionary.TryGetValue(referenceData, out InventoryItem value))
{
value.RemoveFromStack();
if (value.stackSize == 0)
{
inventory.Remove(value);
m_itemDictionary.Remove(referenceData);
}
}
onInventoryChangedEvent.Invoke();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemObject : MonoBehaviour
{
public InventoryItemData referenceItem ;
public void OnHandlePickupItem()
{
InventorySystem.Instance.Add(referenceItem);
Destroy(gameObject);
}
}
using UnityEngine;
public class PickUpItem : MonoBehaviour
{
[SerializeField] private LayerMask itemLayer; // Items must be on this layer
private Camera mainCamera;
void Start()
{
mainCamera = Camera.main;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.P))
{
CheckForItemPickup();
}
}
private void CheckForItemPickup()
{
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hitInfo, Mathf.Infinity, itemLayer))
{
// Item found!
if (hitInfo.transform.TryGetComponent<ItemObject>(out ItemObject item))
{
item.OnHandlePickupItem();
}
}
}
}
please if anyone can help this is a project and im on a time crunch to finish it