S Im following a brakeys tutorial on how to create an inventory system, to the part where we create equipment I was perfectly fine, but when I try to equip the item it works but instead of removing from inventory it dupes it, any idea why?
here’s the code for the item script:
using UnityEngine;
[CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")]
public class Item : ScriptableObject
{
new public string name = "New Item"; // Name of the item
public Sprite icon = null; // Item icon
public bool isDefaultItem = false; // Is the item default wear?
// Called when the item is pressed in the inventory
public virtual void Use ()
{
// Use the item
// Something might happen
Debug.Log("Using " + name);
}
public void RemoveFromInventory ()
{
Inventory.instance.Remove(this);
}
}
EquipmentScript:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Equipment", menuName = "Inventory/Equipment")]
public class Equipment : Item
{
public EquipmentSlot equipSlot; // Slot to store equipment in
public int armorModifier; // Increase/decrease in armor
public int damageModifier; // Increase/decrease in damage
public SkinnedMeshRenderer mesh;
public EquipmentManager.MeshBlendShape[] coveredMeshRegions;
// When pressed in inventory
public override void Use()
{
base.Use();
EquipmentManager.instance.Equip(this); // Equip it
RemoveFromInventory(); // Remove it from inventory
}
}
public enum EquipmentSlot { Head, Chest, Legs, Weapon, Shield, Feet }
InventoryScript:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
#region Singleton
public static Inventory instance;
void Awake ()
{
if (instance != null)
{
Debug.LogWarning("More than one instance of Inventory found!");
return;
}
instance = this;
}
#endregion
// Callback which is triggered when
// an item gets added/removed.
public delegate void OnItemChanged();
public OnItemChanged onItemChangedCallback;
public int space = 20; // Amount of slots in inventory
// Current list of items in inventory
public List<Item> items = new List<Item>();
// Add a new item. If there is enough room we
// return true. Else we return false.
public bool Add (Item item)
{
// Don't do anything if it's a default item
if (!item.isDefaultItem)
{
// Check if out of space
if (items.Count >= space)
{
Debug.Log("Not enough room.");
return false;
}
items.Add(item); // Add item to list
// Trigger callback
if (onItemChangedCallback != null)
onItemChangedCallback.Invoke();
}
return true;
}
// Remove an item
public void Remove (Item item)
{
items.Remove(item); // Remove item from list
// Trigger callback
if (onItemChangedCallback != null)
onItemChangedCallback.Invoke();
}
}
EquipmentManager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EquipmentManager : MonoBehaviour
{
#region Singleton
public enum MeshBlendShape {Torso, Arms, Legs };
public Equipment[] defaultEquipment;
public static EquipmentManager instance;
public SkinnedMeshRenderer targetMesh;
SkinnedMeshRenderer[] currentMeshes;
void Awake ()
{
instance = this;
}
#endregion
Equipment[] currentEquipment; // Items we currently have equipped
// Callback for when an item is equipped/unequipped
public delegate void OnEquipmentChanged(Equipment newItem, Equipment oldItem);
public OnEquipmentChanged onEquipmentChanged;
Inventory inventory; // Reference to our inventory
void Start ()
{
inventory = Inventory.instance; // Get a reference to our inventory
// Initialize currentEquipment based on number of equipment slots
int numSlots = System.Enum.GetNames(typeof(EquipmentSlot)).Length;
currentEquipment = new Equipment[numSlots];
currentMeshes = new SkinnedMeshRenderer[numSlots];
EquipDefaults();
}
// Equip a new item
public void Equip (Equipment newItem)
{
// Find out what slot the item fits in
int slotIndex = (int)newItem.equipSlot;
Equipment oldItem = Unequip(slotIndex);
// An item has been equipped so we trigger the callback
if (onEquipmentChanged != null)
{
onEquipmentChanged.Invoke(newItem, oldItem);
}
// Insert the item into the slot
currentEquipment[slotIndex] = newItem;
AttachToMesh(newItem, slotIndex);
}
// Unequip an item with a particular index
public Equipment Unequip (int slotIndex)
{
Equipment oldItem = null;
// Only do this if an item is there
if (currentEquipment[slotIndex] != null)
{
// Add the item to the inventory
oldItem = currentEquipment[slotIndex];
inventory.Add(oldItem);
SetBlendShapeWeight(oldItem, 0);
// Destroy the mesh
if (currentMeshes[slotIndex] != null)
{
Destroy(currentMeshes[slotIndex].gameObject);
}
// Remove the item from the equipment array
currentEquipment[slotIndex] = null;
// Equipment has been removed so we trigger the callback
if (onEquipmentChanged != null)
{
onEquipmentChanged.Invoke(null, oldItem);
}
}
return oldItem;
}
// Unequip all items
public void UnequipAll ()
{
for (int i = 0; i < currentEquipment.Length; i++)
{
Unequip(i);
}
EquipDefaults();
}
void AttachToMesh(Equipment item, int slotIndex)
{
SkinnedMeshRenderer newMesh = Instantiate(item.mesh) as SkinnedMeshRenderer;
newMesh.transform.parent = targetMesh.transform.parent;
newMesh.rootBone = targetMesh.rootBone;
newMesh.bones = targetMesh.bones;
currentMeshes[slotIndex] = newMesh;
SetBlendShapeWeight(item, 100);
}
void SetBlendShapeWeight(Equipment item, int weight)
{
foreach (MeshBlendShape blendshape in item.coveredMeshRegions)
{
int shapeIndex = (int)blendshape;
targetMesh.SetBlendShapeWeight(shapeIndex, weight);
}
}
void EquipDefaults()
{
foreach (Equipment e in defaultEquipment)
{
Equip(e);
}
}
void Update ()
{
// Unequip all items if we press U
if (Input.GetKeyDown(KeyCode.U))
UnequipAll();
}
}
please if anyone can help it will be much appreciated