I am very new to Unity so please excuse my amateur understanding of things. I am currently trying to implement an interface in unity 3d with an inventory including 2D icons which I want to drag and drop from the inventory which should then let the gameObejct spawn. I have tried to implement the exact codes from Jayanam on YouTube, adding you the links here: Create inventory UI: //www.youtube.com/watch?v=-xB4xEmGtCY&list=PLg7sMWZoap4B8N1pR8nl2-eG_D5k9MfWY
Inventory Script: //www.youtube.com/watch?v=Hj7AZkyojdo&list=PLg7sMWZoap4B8N1pR8nl2-eG_D5k9MfWY&index=2
Inventory UI Drag and Drop: //www.youtube.com/watch?v=Pc8K_DVPgVM&list=PLg7sMWZoap4B8N1pR8nl2-eG_D5k9MfWY&index=3
Drop Item: //www.youtube.com/watch?v=db6ofSbAXnE&list=PLg7sMWZoap4B8N1pR8nl2-eG_D5k9MfWY&index=4
Everything works fine, until I reach the point where I want to drop the Item. I can see that it calls the drop-function, but my gameObject doesn't spawn. I am adding you my codes here and I hope that someone might have the answer.
GameObject:
HUD Script to handle the Inventory:
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class HUD : MonoBehaviour { public Inventory Inventory; // Start is called before the first frame update void Start() { Inventory.ItemAdded += InventoryScript_ItemAdded; Inventory.ItemRemoved += Inventory_ItemRemoved; } private void InventoryScript_ItemAdded(object sender, InventoryEventArgs e) { Transform inventoryPanel = transform.Find("InventoryPanel"); foreach(Transform slot in inventoryPanel) { // Border ... Image Transform imageTransform = slot.GetChild(0).GetChild(0); Image image = slot.GetChild(0).GetChild(0).GetComponent<Image>(); ItemDragHandler itemDragHandler = imageTransform.GetComponent<ItemDragHandler>(); // We found the empty slot if (!image.enabled) { image.enabled = true; image.sprite = e.Item.Image; // TODO: Store a reference to the item itemDragHandler.Item = e.Item; break; } } } private void Inventory_ItemRemoved(object sender, InventoryEventArgs e) { Transform inventoryPanel = transform.Find("InventoryPanel"); foreach (Transform slot in inventoryPanel) { Transform imageTransform = slot.GetChild(0).GetChild(0); Image image = imageTransform.GetComponent<Image>(); ItemDragHandler itemDragHandler = imageTransform.GetComponent<ItemDragHandler>(); //We found the item in the UI if (itemDragHandler.Item.Equals(e.Item)) { image.enabled = false; image.sprite = null; itemDragHandler.Item = null; break; } } } }Item Drag Handler to pull the Item Image out of the Inventory:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class ItemDragHandler : MonoBehaviour, IDragHandler, IEndDragHandler { public IInventoryItem Item { get; set; } public void OnDrag(PointerEventData eventData) { transform.position = Input.mousePosition; } public void OnEndDrag(PointerEventData eventData) { transform.localPosition = Vector3.zero; } }Item Drop Handler:
Inventory Script:
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class Inventory : MonoBehaviour { private const int SLOTS = 4; private List<IInventoryItem> mItems = new List<IInventoryItem>(); public event EventHandler<InventoryEventArgs> ItemAdded; public event EventHandler<InventoryEventArgs> ItemRemoved; //TODO: delete collider, Item should be picked up by mouse public void AddItem(IInventoryItem item) { if(mItems.Count < SLOTS) { Collider collider = (item as MonoBehaviour).GetComponent<Collider>(); if (collider.enabled) { collider.enabled = false; mItems.Add(item); item.OnPickup(); if (ItemAdded != null) { ItemAdded(this, new InventoryEventArgs(item)); } } } } public void RemoveItem(IInventoryItem item) { if (mItems.Contains(item)) { mItems.Remove(item); item.OnDrop(); Collider collider = (item as MonoBehaviour).GetComponent<Collider>(); if (collider != null) { collider.enabled = true; } if (ItemRemoved != null) { ItemRemoved(this, new InventoryEventArgs(item)); } } } }Inventory Item Class
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public interface IInventoryItem { string Name { get; } Sprite Image { get; } void OnPickup(); void OnDrop(); } public class InventoryEventArgs: EventArgs { public InventoryEventArgs(IInventoryItem item) { Item = item; } public IInventoryItem Item; }I would be really grateful if someone could help me out. I have been trying forever and I'm going crazy.