- Home /
Can't figure out how to code equipment slots
I'm trying to make the item icon in my equipment slots show up when an item is equipped, and I can't find a way to reference the other scripts in my EquipSlot script. I tried maybe referencing it as an instance in the Equipment script, but it gave me an error. EquipSlot Script:
using UnityEngine; using UnityEngine.UI;
public class EquipSlot : MonoBehaviour {
EquipmentSlot equipSlot; //Slot to store equipment in
public Image icon;
public Button removeButton;
Equipment equipment;
Item item;
InventorySlot invSlot;
public bool isEquipped;
public void EquipItem(Item newItem, Equipment Equip)
{
item = newItem;
icon.sprite = item.icon;
icon.enabled = true;
removeButton.interactable = true;
}
public void ClearSlot()
{
item = null;
icon.sprite = null;
icon.enabled = false;
removeButton.interactable = false;
}
public void OnRemoveButton()
{
//Remove from equipment slot
}
}
Equipment scripts:
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 SkinnedMeshRenderer mesh;
public int armorModifier; // Increase/decrease in armor
public int damageModifier; //Increase/decrease in damage
public override void Use()
{
base.Use();
EquipmentManager.instance.Equip(this);
RemoveFromInventory();
}
public void Equip()
{
EquipmentManager.instance.Equip(this); // Equip it
RemoveFromInventory(); // Remove from inventory
}
}
public enum EquipmentSlot { Head, Body, Face, Back, Waist, Hands, Weapon, Leg, Foot }
Answer by MaishyN · May 16, 2018 at 11:02 PM
At first it is really confusing to have a script named EquipSlot and an enum named EquipmentSlot which has an instance named equipSlot.
About these references, it's not entirely clear what you're asking for. For classes that don't inherit from MonoBehaviour you can reference them by making them Serializable, so you can edit instances via Inspector (with fields corresponding to them with [SerializeField] attribute or public), initialize all instances by calling the constructor in Start()/Awake() or keep them in a separate database object that would load corresponding data from a scriptable object or a data file (like xml/json). Classes inheriting from MonoBehavior should have other references attached with the inspector.
I'm not sure I understand...this is a concept that I'm still learning though, but would I reference the equipment script in EquipSlot script or vice versa?
Just as you wrote, you should reference the Equipment inside EquipSlot script. The easiest way would be to make the Equipment ScriptableObject, make the equipment field public or give it [SerializeField] attribute and attach the ScriptableObject file via inspector. It would be easier to clarify if whole class hierarchy was shown with an outline of Scene composition with GameObjects and UI.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
How can i open in game manu (GUI) with button? 2 Answers
How to create elements array about all visible in GUI Editor? 1 Answer
Adding score when enemy dies (Errors) 2 Answers