Hi there, I have the following code working for a drag and drop equipment system. However for some reason when I swap a piece of equipment (rather than dragging it into an empty slot) its bool for isEquipped does not change (nor on the piece of equipment being unequipped).
The Equip () only works correctly if there is not already an item in the slot. I’m sure it’s a reference issue but after a while of scanning my code i’m at a loss. Hope someone can help, thanks!
public GameObject item
{
get
{
if (transform.childCount > 0)
{
return transform.GetChild (0).gameObject;
}
return null;
}
}
private List<BaseItem> invent;
private void Awake ()
{
invent = GameObject.FindGameObjectWithTag ("InventoryManager").GetComponent<Inventory> ().inventory;
}
private void Equip ()
{
DragHandler.item.transform.SetParent (transform);
DragHandler.item.GetComponent<LayoutElement> ().ignoreLayout = false;
string itemName = item.GetComponent<Image> ().sprite.name;
//Set to equipped
for (int i = 0; i < invent.Count; i++)
{
if (invent [i].itemIcon.name == itemName)
{
invent [i].isEquipped = true;
}
}
}
private void Unequip ()
{
string itemName = item.GetComponent<Image> ().sprite.name;
//Set to unequipped
for (int i = 0; i < invent.Count; i++)
{
if (invent [i].itemIcon.name == itemName)
{
invent [i].isEquipped = false;
}
}
DestroyUnequipped ();
}
private void DestroyUnequipped ()
{
foreach (Transform child in transform)
{
Destroy (child.gameObject);
}
}
#region IDropHandler implementation
public void OnDrop (PointerEventData eventData)
{
if (!item)
{
Equip ();
} else
{
Unequip ();
Equip ();
}
DragHandler.item.GetComponent<DragHandler> ().enabled = false;
}
#endregion
}
on line 47 in the above code, it debugs the correct name, and also isEquipped == false. However, in my list in the inspector (inventory manager) the item still shows as equipped…
Is it possible to have more than one item with the same sprite name in your inventory list?
Honestly, your code confuses me a little as I don’t understand why you’re looping through your inventory instead of just accessing the two items that need to be equipped/unequipped.
I’m not too sure I know what you mean. Do you mean I should just use List.Find instead? My item being dragged doesn’t hold any reference to the item it represents as all my inventory items are instantiated from a single prefab, and then the correct sprite is applied. This is my first attempt at an inventory so forgive me if what I have attempted is far from good. Let me give you a rough outline of what I have, and want to achieve.
I have a BaseItem class that has various constructors for my items.
These get added to an ItemDatabase list at runtime.
My inventory is another list of BaseItems.
My drag and drop system currently moves a transform from my inventory to an equipment slot. There is no reference to the BaseItem hence the reason i’m having to compare the sprite names with the items in the inventory.
My inventory system is slightly different to the tutorials I see as I’m developing a mobile game and I won’t have an “in-play” inventory. The player will choose their loadout before starting a level, and in the background an inventory manager will keep track of the items they gain.
Is there a better way to go about this? I find myself having to constantly use loops to match my Instantiated objects to the correct items. Here is my DragHandler script so you can better see what I am attempting to create:
DragHandler
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class DragHandler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public static Transform item; //Inventory item prefab, item being dragged, need to change this to get the item from inventory list
private Transform tempCopy;
private Transform startParent;
private Vector3 startPosition;
private Vector3 newPos;
private Transform tempParent;
private Inventory inventory;
private void Awake ()
{
tempParent = GameObject.FindGameObjectWithTag ("TempItemParent").GetComponent<Transform> ();
}
private void CreateCopy ()
{
tempCopy = Instantiate (item, startParent);
}
#region IBeginDragHandler implementation
public void OnBeginDrag (PointerEventData eventData)
{
item = transform;
//Get item information
string itemSpriteName = transform.GetComponent<Image> ().sprite.name;
BaseItem itemToGrab = inventory.GetItemBySpriteName (itemSpriteName);
startPosition = transform.position;
startParent = transform.parent;
GetComponent<CanvasGroup> ().blocksRaycasts = false;
CreateCopy ();
}
#endregion
#region IDragHandler implementation
public void OnDrag (PointerEventData eventData)
{
transform.SetParent (tempParent);
RectTransformUtility.ScreenPointToWorldPointInRectangle(this.GetComponent<RectTransform>(), eventData.position, eventData.enterEventCamera, out newPos);
transform.position = newPos;
}
#endregion
#region IEndDragHandler implementation
public void OnEndDrag (PointerEventData eventData)
{
transform.position = newPos;
item = null;
GetComponent<CanvasGroup> ().blocksRaycasts = true;
if (transform.parent == tempParent)
{
foreach (Transform child in startParent)
{
Destroy (child.gameObject);
}
transform.SetParent (startParent);
transform.position = startPosition;
}
}
#endregion
}