Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug grid map doesnt work

Discussion in 'Scripting' started by ItsMe1423, Apr 4, 2024.

  1. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    I have these two scripts
    script a
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5. public class MapSlot : MonoBehaviour, IDropHandler
    6. {
    7.     public void OnDrop(PointerEventData eventData)
    8.     {
    9.         GameObject dropped = eventData.pointerDrag;
    10.         DragDrop dragdrop = dropped.GetComponent<DragDrop>();
    11.         dragdrop.parentAfterDrag = transform;
    12.         Debug.Log(transform);
    13.     }
    14. }
    15.  
    script b:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.EventSystems;
    6.  
    7. public class DragDrop : MonoBehaviour, IPointerDownHandler, IBeginDragHandler, IEndDragHandler, IDragHandler
    8. {
    9.     private RectTransform RectTransform;
    10.     [SerializeField] private Canvas canvas;
    11.     [HideInInspector] public Transform parentAfterDrag;
    12.     public Image image;
    13.  
    14.     private void Awake()
    15.     {
    16.         RectTransform = GetComponent<RectTransform>();
    17.     }
    18.     public void OnBeginDrag(PointerEventData eventData)
    19.     {
    20.         parentAfterDrag = transform.parent;
    21.         transform.SetParent(transform.root);
    22.         image.raycastTarget = false;
    23.     }
    24.  
    25.     public void OnDrag(PointerEventData eventData)
    26.     {
    27.         RectTransform.anchoredPosition += eventData.delta / canvas.scaleFactor;
    28.     }
    29.  
    30.     public void OnEndDrag(PointerEventData eventData)
    31.     {
    32.         transform.SetParent(parentAfterDrag);
    33.         image.raycastTarget = true;
    34.     }
    35.  
    36.     public void OnPointerDown(PointerEventData eventData)
    37.     {
    38.     }
    39. }
    40.  
    and these two game objects:
    game object A which is the player icon:
    upload_2024-4-4_18-50-56.png

    and game object B which is the map slot:
    upload_2024-4-4_18-51-24.png

    can anyone see why isn't it working? the problem is that I can't switch between the slots, the drag and everything else works just fine

    EDIT: what actually happens is that it just snaps right back to the original slot
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,669
    Your code suppose that OnDrop always happens before OnEndDrag, Is that actually what's happening, though?
     
  3. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    yes, because on drop happens when you drop the player icon onto one of the map slots
     
  4. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    Ok I am back home now and I have done some more testing and it doesn't even seem to go into the OnDrop(), I have added a debug.log() and I don't get anything in the console