Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Help with weapon inventory system

Discussion in 'Scripting' started by XAlwalidX, Apr 2, 2024.

  1. XAlwalidX

    XAlwalidX

    Joined:
    Dec 2, 2021
    Posts:
    2
    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

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. [CreateAssetMenu( menuName = "Iventory Item Data")]
    7. public class InventoryItemData : ScriptableObject
    8. {
    9.    public string id;
    10.    public string displayName;
    11.    public Sprite icon;
    12.    public GameObject prefab;
    13.  
    14.    public ItemType itemType;
    15.    public int damage;
    16.    public float attackSpeed;
    17.  
    18.  
    19.  
    20.  
    21.    public enum ItemType
    22.    {
    23.     weapon,
    24.     armor,
    25.     consumable,
    26.     other
    27.    }
    28. }
    29.  
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6.  
    7. public class InventorySystem: MonoBehaviour
    8. {
    9.     private static InventorySystem _instance;
    10.  
    11.     public static InventorySystem Instance
    12.     {
    13.         get
    14.         {
    15.             if (_instance == null)
    16.             {
    17.                 _instance = FindObjectOfType<InventorySystem>();
    18.                 if (_instance == null)
    19.                 {
    20.                     Debug.LogError("An InventorySystem instance wasn't found in the scene");
    21.                 }
    22.             }
    23.  
    24.             return _instance;
    25.         }
    26.     }
    27.   public Dictionary<InventoryItemData, InventoryItem> m_itemDictionary;
    28.   public List<InventoryItem> inventory { get; private set; }
    29.  
    30.    public event Action  onInventoryChangedEvent;  // Event for inventory change updates
    31.  
    32.   private void Awake()
    33. {
    34.    inventory = new List<InventoryItem>();
    35.    m_itemDictionary = new Dictionary<InventoryItemData, InventoryItem>();
    36. }
    37.   public void Add (InventoryItemData referenceData)
    38.   {
    39.     if(m_itemDictionary.TryGetValue(referenceData, out InventoryItem value))
    40.     {
    41.         value.AddToStack();
    42.     }
    43.     else
    44.     {
    45.      InventoryItem newItem = new InventoryItem(referenceData);
    46.      inventory.Add(newItem);
    47.      m_itemDictionary.Add(referenceData, newItem);
    48.     }
    49.     onInventoryChangedEvent.Invoke();
    50.   }
    51.   public void Remove (InventoryItemData referenceData)
    52.   {
    53.      if (m_itemDictionary.TryGetValue(referenceData, out InventoryItem value))
    54.      {
    55.          value.RemoveFromStack();
    56.  
    57.          if (value.stackSize == 0)
    58.          {
    59.            inventory.Remove(value);
    60.            m_itemDictionary.Remove(referenceData);
    61.          }
    62.      }
    63.       onInventoryChangedEvent.Invoke();
    64.   }
    65. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ItemObject : MonoBehaviour
    6. {
    7.     public InventoryItemData referenceItem ;
    8.  
    9.     public void OnHandlePickupItem()
    10.     {
    11.         InventorySystem.Instance.Add(referenceItem);
    12.         Destroy(gameObject);
    13.     }
    14. }
    15.  
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PickUpItem : MonoBehaviour
    4. {
    5.     [SerializeField] private LayerMask itemLayer; // Items must be on this layer
    6.     private Camera mainCamera;
    7.  
    8.     void Start()
    9.     {
    10.         mainCamera = Camera.main;
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         if (Input.GetKeyDown(KeyCode.P))
    16.         {
    17.             CheckForItemPickup();
    18.         }
    19.     }
    20.  
    21.     private void CheckForItemPickup()
    22.     {
    23.         Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
    24.  
    25.         if (Physics.Raycast(ray, out RaycastHit hitInfo, Mathf.Infinity, itemLayer))
    26.         {
    27.             // Item found!
    28.             if (hitInfo.transform.TryGetComponent<ItemObject>(out ItemObject item))
    29.             {
    30.                 item.OnHandlePickupItem();
    31.             }
    32.         }
    33.     }
    34. }

    please if anyone can help this is a project and im on a time crunch to finish it
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,005
    Fastest way will definitely be to start from a short simple inventory tutorial.

    Most of what goes into an inventory won't be the code so much as the setup and design of prefabs and any other things you might use like ScriptableObjects to define your content.

    Since inventories are one of the most complicated things you can do, make sure you set a modest goal for your first inventory, perhaps something along the lines of a hotkey bar such as what the first Diablo had. The more features you support, the more abstract and complicated it gets, and it gets complicated fast.

    Anything you accomplish will be done one step at a time, just the same way this guy does it:

    Imphenzia: How Did I Learn To Make Games:

     
  3. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,470
    Here are two tutorials i will recommend for RPG stuff like you are looking for:

    Option 1:
    (518) CREATE DARK SOULS IN UNITY - YouTube

    Option 2:
    upload_2024-4-2_16-29-35.png

    Pros of Option 1:
    -Very comprehensive
    -Good C# knowledge

    Cons of Option 1:
    -Can be a lot for beginners (definitely not impossible)
    -He goes VERY fast in some parts and tends to hide functions in VS after typing them
    -If you miss a video, you could be missing some necessary parts from the ones you skipped

    Pros of Option 2:
    -Pretty comprehensive
    -Great C# knowledge
    -Beginner friendly (with some more intermediate concepts)

    Cons of Option 2:
    -Some videos are too slow and long
    -If you skip a video, you may be needing something from it later (most long tutorial series are like this)
    -His Saving/Loading system is outdated. It works, but don't do it that way, use the one from Option 1, if anything

    Note, both can (mostly) be adapted to 3D or 2D. Both tutorials cover what you are looking for. Good luck
     
    Kurt-Dekker likes this.