Newbie stumped with an inventory bug
So, I’ll include the code down below as well, and if I missed anything. I had a video but because I’m a new user it won’t let me. I’m relatively new to unity. I’ve got experience with programming, but this is my first ‘game’ in 10 years. So I decided to go through a tutorial to re-familiarise myself with things, and learn the basics of the system. However, the tutorial I used was for an earlier version of Unity, and it’s possible I also made a mistake somewhere. Either way, for some reason the Dragable script, which I forgot to show in the video, doesn’t seem to detect the leftmost row in the inventory UI slots. I’ll link the files below, and all of that. It’s not a big huge thing since this is more for learning, but it’s been bugging me for two weeks now because I can’t figure out what’s causing this. Thanks for the help! ItemDragable: using UnityEngine; using UnityEngine.EventSystems; public class ItemDragable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { Transform originalParent; CanvasGroup canvasGroup; public float minDropDistance = 2f; public float maxDropDistance = 3f; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { canvasGroup = GetComponent (); } public void OnBeginDrag(PointerEventData eventData) { originalParent = transform.parent; // Save Original Parent transform.SetParent(transform.root); //I think this means 'make item on top' canvasGroup.blocksRaycasts = false; // makes it clickable. In this case: You're already clicking. canvasGroup.alpha = 0.6f; // Semi transparrent during drag. } public void OnDrag(PointerEventData eventData) { transform.position = eventData.position; //Item moves where dragged. Could be useful for coding follower. } public void OnEndDrag(PointerEventData eventData) { canvasGroup.blocksRaycasts = true; // makes it clickable. In this case: so you can drag again. canvasGroup.alpha = 1f; Slot dropSlot = eventData.pointerEnter?.GetComponent (); // Question mark makes a variable able to be null. Useful! Slot originalSlot = originalParent.GetComponent (); if (dropSlot == null) { GameObject dropItem = eventData.pointerEnter; if (dropItem!=null) { dropSlot = dropItem.GetComponentInParent (); // Gets the item below's slot. Because something something grabbing the raycast? } } if (dropSlot != null) { if(dropSlot.currentItem != null) { dropSlot.currentItem.transform.SetParent(originalSlot.transform); originalSlot.currentItem = dropSlot.currentItem; dropSlot.currentItem.GetComponent ().anchoredPosition = Vector2.zero; // If there's an item in the slot, move the item to the previous slot. } else { originalSlot.currentItem = null; // If there isn't an item...empty old slot of item. } transform.SetParent(dropSlot.transform); dropSlot.currentItem = gameObject; //hotbarController.SetHotbarItem(saveData.InventorySaveData); // Moves the dragged item to the new slot. } else { //If where we're dropping is outside the menu, drop the item. if (!IsWithinInventory(eventData.position)) { DropItem(originalSlot); } else { transform.SetParent(originalParent); // Return to old slot. } } GetComponent ().anchoredPosition = Vector2.zero; // Center } bool IsWithinInventory(Vector2 mousePosition) { RectTransform inventoryRect = originalParent.parent.GetComponent (); return RectTransformUtility.RectangleContainsScreenPoint(inventoryRect, mousePosition); } ///throw new System.NotImplementedException(); void DropItem(Slot originalSlot) { originalSlot.currentItem = null; // Find player Transform playerTransform = GameObject.FindGameObjectWithTag("Player")?.transform; if (playerTransform == null) { Debug.LogError("Missing 'Player' tag."); return; } // Random drop position Vector2 dropOffset = Random.insideUnitCircle.normalized * Random.Range(minDropDistance, maxDropDistance); Vector2 dropPosition = (Vector2)playerTransform.position + dropOffset; //instantiate drop item and bounce GameObject dropItem = Instantiate(gameObject, dropPosition, Quaternion.identity); dropItem.GetComponent ().StartBounce(); //destroy the ui one Destroy(gameObject); } } Slot: using UnityEngine; public class Slot : MonoBehaviour { public GameObject currentItem; //The item that's held in this slot. } InventoryController: using UnityEngine; using System.Collections; using System.Collections.Generic; public class InventoryController : MonoBehaviour { private ItemDictionary itemDictionary; private ItemDragable itemdraggable; public GameObject inventoryPanel; public GameObject hotbarPanel; public GameObject slotPrefab; public int slotCount; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { //itemDictionary = FindObjectOfType (); itemDictionary = Object.FindAnyObjectByType (); // Creates the inventory slots. Removed as it's now done in SetInventoryItems. /* for (int i = 0; i (); if (i ().anchoredPosition = Vector2.zero; slot.currentItem = item; } } */ } public bool AddItem(GameObject itemPrefab) { foreach (Transform slotTransform in inventoryPanel.transform) { // For each slot in the inventory menu Slot slot = slotTransform.GetComponent (); if (slot != null && slot.currentItem == null) { GameObject newItem = Instantiate(itemPrefab, slotTransform); newItem.GetComponent ().anchoredPosition = Vector2.zero; slot.currentItem = newItem; return true; } } Debug.Log("Inventory is Full!"); return false; } public List GetInventoryItems() { List invData = new List (); foreach (Transform slotTransform in inventoryPanel.transform) { Slot slot = slotTransform.GetComponent (); if (slot.currentItem != null) { Item item = slot.currentItem.GetComponent (); invData.Add(new InventorySaveData {itemID = item.ID, slotIndex = slotTransform.GetSiblingIndex() }); } } return invData; } public void SetInventoryItem(List inventorySaveData) { // Clear the empty slots made on game start to make way for the new data. foreach (Transform child in inventoryPanel.transform) { Destroy(child.gameObject); } //Create New data slots for (int i = 0; i (); if (itemPrefab != null) { GameObject item = Instantiate(itemPrefab, slot.transform); item.GetComponent ().anchoredPosition = Vector2.zero; slot.currentItem = item; } } else { Debug.LogWarning($"Inventory amount {data.slotIndex} does not fit in slot amount."); } } } } And just in case, Item, and Item dictionary: using UnityEngine; using UnityEngine.UI; public class Item : MonoBehaviour { public int ID; public string Name; public int Amount = 1; public virtual void UseItem() { Debug.Log($"Using item {Name}."); } public virtual void Pickup() { Sprite itemIcon = GetComponent ().sprite; if (ItemPickupUIController.Instance != null) { //Stops it crashing if an item doesn't exist. ItemPickupUIController.Instance.ShowItemPickup(Name, itemIcon); } } } using System.Collections; using System.Collections.Generic; using UnityEngine; public class ItemDictionary : MonoBehaviour { public List itemPrefabs; private Dictionary itemDictionary; private void Awake() { itemDictionary = new Dictionary (); //Loads all items into the dictionary for (int i = 0; i 4 posts - 2 participants Read full topic










