• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ealva479 · May 30, 2018 at 03:53 AM · inventory systemequipment

Equipment Slots not completely working

I made some progress on getting my equipment slots to work, but not completely. Can't seem to figure this part out. At runtime, the (blank) item icons are active, and if I equip any item, they go inactive. Except there are 2 items that show up on ALL of the slots. See in the video: https://youtu.be/MhbSX2S9R-U

Here is my equipment slot code:

using UnityEngine; using UnityEngine.UI;

public class EquipSlot : MonoBehaviour {

 public EquipmentSlot equipmentSlot;

 EquipUI UI;

 public Equipment equipment;

 public Image icon;
 public Button removeButton;

 Item item;
 
 public bool isEquipped;

 public void Start()
 {
     EquipmentManager.instance.onEquipmentChanged += OnEquipmentChanged;
 }
 
 public void EquipItem(Equipment equipment)
 {
     item = equipment;

         icon.sprite = item.icon;
         icon.enabled = true;
         this.equipment = equipment; //reference to equipped item
         removeButton.interactable = true;
 }

 public void ClearSlot()
 {
     item = null;

     icon.sprite = null;
     icon.enabled = false;
     removeButton.interactable = false;

 }

 public void OnRemoveButton()
 {
     //Remove from equipment slot
 }

public void OnEquipmentChanged(Equipment newItem, Equipment oldItem) { if (newItem != null) { item = equipment;

         icon.sprite = newItem.icon;
         icon.enabled = true;
         removeButton.interactable = true;
         //this.equipment = equipment;
     }

     if (oldItem != null)
     {
         item = null;
         
         icon.sprite = null;
         icon.enabled = false;
         removeButton.interactable = false;
     }
 }   

}

Equipment Manager:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class EquipmentManager : MonoBehaviour {

 #region Singleton
 public static EquipmentManager instance;

 private void Awake()
 {
     instance = this;
 }
 #endregion

 public List<Equipment> items = new List<Equipment>();
 
 public Equipment[] defaultItems;
 public SkinnedMeshRenderer targetMesh;
 public Equipment[] currentEquipment; //Items we currently have equipped
 SkinnedMeshRenderer[] currentMeshes;

 //Callback for when an item is equipped/unequipped
 public delegate void OnEquipmentChanged(Equipment newItem, Equipment oldItem);
 public OnEquipmentChanged onEquipmentChanged;

 Inventory inventory; //Ref to inventory

 private void Start()
 {
     inventory = Inventory.instance; //Get ref to 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];

     EquipDefaultItems();
 }

 //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;
     SkinnedMeshRenderer newMesh = Instantiate<SkinnedMeshRenderer>(newItem.mesh);
     newMesh.transform.parent = targetMesh.transform;

     newMesh.bones = targetMesh.bones;
     newMesh.rootBone = targetMesh.rootBone;
     currentMeshes[slotIndex] = newMesh;
 }

 //Unequip an item with a particular index
 public Equipment Unequip (int slotIndex)
 {
     //Only do this if an item is there
     if (currentEquipment [slotIndex] != null)
     {
         if(currentMeshes[slotIndex] != null)
         {
             Destroy(currentMeshes[slotIndex].gameObject);
         }
         //Add the item to the inventory
         Equipment oldItem = currentEquipment[slotIndex];
         inventory.Add(oldItem);

         //Remove the item from the equipment array
         currentEquipment[slotIndex] = null;

         //Equipment has been removed to we trigger the callback
         if (onEquipmentChanged != null)
         {
             onEquipmentChanged.Invoke(null, oldItem);
         }
         return oldItem;
     }
     return null;
 }

 public void UnequipAll()
 {
     for (int i = 0; i < currentEquipment.Length; i++)
     {
         Unequip(i);
     }
     EquipDefaultItems();
 }

 void EquipDefaultItems()
 {
     foreach(Equipment item in defaultItems)
     {
         Equip(item);
     }
 }

 void Update()
 {
     if (Input.GetKeyDown(KeyCode.U))
         UnequipAll();
 }

}

EquipUI:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class EquipUI : MonoBehaviour {

 public Transform EquipSlots;
 
 Inventory inventory;
 EquipmentManager manager;

 public EquipSlot slot;
 
 EquipSlot[] eSlots;
 
 // Use this for initialization
 void Start () {
     manager = EquipmentManager.instance;
     //manager.onEquipmentChanged += UpdateUI;

     eSlots = EquipSlots.GetComponentsInChildren<EquipSlot>();
    
 }
 
 // Update is called once per frame
 void Update () {
     
 }

 void UpdateUI()
 {      
     
     for (int i = 0; i < eSlots.Length; i++)
     {
         if(i < manager.items.Count)
         {
             eSlots[i].EquipItem(manager.items[i]);
         } else
         {
             eSlots[i].ClearSlot();
         } 
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

82 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I need to find inventory and item system, and to save them 0 Answers

Character Equipment (clothes/weapons/armor) 1 Answer

How to access the texture of a grid space the player clicked on in an inventory system created using GUI.SelectionGrid? 0 Answers

add Object to inventory GUI 1 Answer

Equipping weapons with inventory system? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges