Search code examples
parent-childunity-ui

how to change the parent of an object to another slot parent in unity?


i am making a rock paper scissors game but not the classic one that everyone knows, you have 12 cards, 3 blue cards of each type(3 paper cards,3 rock cards,3 scissors cards), 2 purple cards of each type and one last card that contains paper and scissors at the same time and for the game to start you have to arange those cards in slots.here an image from the game after a lot of thinking i made it work with drag&drop system but i faced some problems:

  • first problem that everytime i try to drop any card into the slot it returns to it's original position .
  • second problem that i want the slot to become the parent of the card inserted in it but it just leave it's parent when i start dragging it then when i release it it returns to it's original parent(i even tried to delete some lines in my script to test if the second problem is caused by the first one or not,but it's the same even thought that the card get implemented in the slot and those codes aren't for nothing they return the card to it's original position if it's not implemented in a slot ). here the cards code:
using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class DragDrop : MonoBehaviour, IPointerDownHandler,IBeginDragHandler,IEndDragHandler,IDragHandler
 {
     private RectTransform rectTransform;
     private CanvasGroup canvasGroup;
     public Text textField;
     [SerializeField] Canvas canvas;
     private Transform originalPos;
 
 
     private void Awake()
     {   
         rectTransform = GetComponent<RectTransform>();
         canvasGroup = GetComponent<CanvasGroup>();
     }
 
 
     public void OnBeginDrag(PointerEventData eventData)
     {
         canvasGroup.alpha = .5f;
         canvasGroup.blocksRaycasts = false;
         originalPos = transform.parent;
         transform.SetParent(transform.parent.parent);
     }
 
     public void OnDrag(PointerEventData eventData)
     {
         rectTransform.anchoredPosition += eventData.delta / canvas.scaleFactor;
     }
 
     public void OnEndDrag(PointerEventData eventData)
     {
         canvasGroup.alpha = 1f;
         canvasGroup.blocksRaycasts = true;
         transform.SetParent(originalPos);
         transform.localPosition = Vector3.zero;
         if (transform.localPosition == Vector3.zero)
         {
             textField.enabled = true;
         }
         
 
     }
 
     public void OnPointerDown(PointerEventData eventData)
     {
         
         
         textField.enabled = false;
         
         
     }
 }

and here the slots code:

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class slots : MonoBehaviour, IDropHandler
 
 {
     public GameObject slot;
     public void OnDrop(PointerEventData eventData)
     {
         if(eventData.pointerDrag != null) 
         {
             eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition = GetComponent<RectTransform>().anchoredPosition;
         }
     }
 }

i really appreciate anything you can help me with cause i've been stuck in this for 2 days now.


Solution

  • It looks to me as if you're setting the Transform.parent back to the originalPos there in your OnEndDrag method. You should probably do a MouseToWorldPoint and find the object you're hovering over, then set the parent to that gameObjects transform.