Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Trying to combine my inventory and weapon system

Discussion in 'Scripting' started by ziyo100, May 10, 2021.

  1. ziyo100

    ziyo100

    Joined:
    Apr 13, 2021
    Posts:
    6
    So I watched some tutorials and made a inventory and weapon system but the problem is these system were both taken from different kind of videos. So I want to combine both of them.

    this is my weapon switch system
    Code (CSharp):
    1.     using UnityEngine;
    2.    
    3.     public class Weapon_switching : MonoBehaviour
    4.     {
    5.         public int selectedWeapon = 0;
    6.    
    7.         // Start is called before the first frame update
    8.         void Start()
    9.         {
    10.             SelectWeapon();
    11.         }
    12.    
    13.         // Update is called once per frame
    14.         void Update()
    15.         {
    16.             int previousSelectedWeapon = selectedWeapon;
    17.    
    18.             if (Input.GetAxis("Mouse ScrollWheel") > 0f)
    19.             {
    20.                 if (selectedWeapon >= transform.childCount - 1)
    21.                     selectedWeapon = 0;
    22.                 else
    23.                 selectedWeapon++;
    24.             }
    25.             if (Input.GetAxis("Mouse ScrollWheel") < 0f)
    26.             {
    27.                 if (selectedWeapon <= 0)
    28.                     selectedWeapon = transform.childCount - 1;
    29.                 else
    30.                     selectedWeapon--;
    31.             }
    32.    
    33.             if(Input.GetKeyDown(KeyCode.Alpha1))
    34.             {
    35.                 selectedWeapon = 0;
    36.             }
    37.        
    38.             if (Input.GetKeyDown(KeyCode.Alpha2) && transform.childCount >= 2)
    39.             {
    40.                 selectedWeapon = 1;
    41.             }
    42.    
    43.             if (Input.GetKeyDown(KeyCode.Alpha3) && transform.childCount >= 3)
    44.             {
    45.                 selectedWeapon = 2;
    46.             }
    47.    
    48.             if (Input.GetKeyDown(KeyCode.Alpha2) && transform.childCount >= 4)
    49.             {
    50.                 selectedWeapon = 3;
    51.             }
    52.    
    53.             if (Input.GetKeyDown(KeyCode.Alpha2) && transform.childCount >= 5)
    54.             {
    55.                 selectedWeapon = 4;
    56.             }
    57.    
    58.             if (previousSelectedWeapon != selectedWeapon)
    59.             {
    60.                 SelectWeapon();
    61.             }
    62.         }
    63.    
    64.         void SelectWeapon ()
    65.         {
    66.             int i = 0;
    67.             foreach (Transform weapon in transform)
    68.             {
    69.                 if (i == selectedWeapon)
    70.                     weapon.gameObject.SetActive(true);
    71.                 else
    72.                     weapon.gameObject.SetActive(false);
    73.                 i++;
    74.             }
    75.         }
    76.     }
    77.  
    this is the equipment code
    Code (CSharp):
    1.     using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.    
    5.     [CreateAssetMenu(fileName = "New Equipment", menuName = "Inventory/Equipment")]
    6.     public class Equipment : Item
    7.     {
    8.         public EquipmentSlot equipSlot;
    9.    
    10.         public int armorModifier;
    11.    
    12.         public override void Use()
    13.         {
    14.             base.Use();
    15.             EquipmentManager.instance.Equip(this);
    16.             RemoveFromInventory();
    17.         }
    18.     }
    19.    
    20.     public enum EquipmentSlot { Armor, Smg, Pistol, Rifle, Heavy}
    21.  
    this is the equipment manager
    Code (CSharp):
    1.     using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.    
    5.     public class EquipmentManager : MonoBehaviour
    6.     {
    7.         #region Singleton
    8.         public static EquipmentManager instance;
    9.    
    10.         private void Awake()
    11.         {
    12.             instance = this;
    13.         }
    14.    
    15.         #endregion
    16.    
    17.         Equipment[] currentEquipment;
    18.    
    19.         public delegate void OnEquipmentChanged(Equipment newItem, Equipment oldItem);
    20.         public OnEquipmentChanged onEquipmentChanged;
    21.    
    22.         Inventory inventory;
    23.    
    24.         private void Start()
    25.         {
    26.             inventory = Inventory.instance;
    27.    
    28.             int numSlots = System.Enum.GetNames(typeof(EquipmentSlot)).Length;
    29.             currentEquipment = new Equipment[numSlots];
    30.         }
    31.         public void Equip (Equipment newItem)
    32.         {
    33.             int slotIndex = (int)newItem.equipSlot;
    34.    
    35.             Equipment oldItem = null;
    36.    
    37.             if (currentEquipment[slotIndex] != null)
    38.             {
    39.                 oldItem = currentEquipment[slotIndex];
    40.                 inventory.Add(oldItem);
    41.             }
    42.    
    43.             if (onEquipmentChanged != null)
    44.             {
    45.                 onEquipmentChanged.Invoke(newItem, oldItem);
    46.             }
    47.    
    48.             currentEquipment[slotIndex] = newItem;
    49.         }
    50.    
    51.     }
    52.  
    I wanna make it so when I press 1 on the keyboard it will equip the smg and pistol when I press 2 and be switchable with the mouse scrollwheel too
    I am new at the game making stuff and still trying to learn and I would be very happy if someone could help me.
    edit: forgot to say, I want to abandon the old method and switch between my weapons using the equipment system
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    You might be better off just choosing which of the above works better for you (Weapon or Equipment), then just deleting the other one while putting all of its features into the first one.

    You need to identify what each one does for you that is different first though, obviously.

    Be sure to separate user intentions (what the user clicks or presses) from the meaning (select next weapon) from the processing of that intent (actually changing the internal data representing what weapon is active).
     
  3. ziyo100

    ziyo100

    Joined:
    Apr 13, 2021
    Posts:
    6
    Thanks for the advice