- Home /
how to change the parent of an object to another slot parent ?
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. ![alt text][1] 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
in end drag you have transform.SetParent(originalPos); but you want to set the parent to the new position not the original.. and you don't get the transform for the new position anywhere in your code. You need to get the transform of the slot you want to be the new parent and then use SetParent with that transform. transform.SetParent(newPos);
Your answer
Follow this Question
Related Questions
SetParent does not work. 1 Answer
Draggable in Scroll View Returning to proper place on Drop 0 Answers
Cannot Drag / Drop png into Sprite Renderer 2 Answers
Drag and Drop lagging behind mouse 0 Answers
unity 2d drag & drop 1 Answer