- Home /
Need help debugging: SiblingIndex
Hi there!
Trying to create a card game. Player should be able to rearrange the cards in their hand. To show where the card the player is currently dragging was before I create a placeholder. The placeholder is destroyed at the end of the drag. Everything runs pretty smooth, but when I rearrange the cards for a while it gets a little chaotic: at the end I always have 1 card that is at a different SiblingIndex as it should be. Would be lovely if you could take a look please.
Here's the code: (every GameObject Card got a Script(c#) called Draggable)
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public Transform parentToReturnTo = null;
public Transform placeholderParent = null;
public GameObject placeholder = null;
//public GameObject preFab;
public void OnBeginDrag (PointerEventData eventData)
{
placeholder = new GameObject ();
//placeholder = Instantiate (preFab) as GameObject;
parentToReturnTo = this.transform.parent;
placeholder.transform.SetParent (parentToReturnTo);
placeholder.transform.SetSiblingIndex (this.transform.GetSiblingIndex ());
placeholder.transform.localPosition = this.transform.localPosition;
placeholder.transform.localRotation = this.transform.localRotation;
this.transform.SetParent (this.transform.parent.parent);
this.GetComponent<CanvasGroup> ().blocksRaycasts = false;
}
public void OnDrag (PointerEventData eventData)
{
this.transform.position = eventData.position;
placeholderParent = placeholder.transform.parent;
int tempSI = placeholderParent.childCount-1;
for (int i=0; i < placeholderParent.childCount; i++) {
if (this.transform.position.x < placeholderParent.GetChild (i).position.x) {
tempSI = i;
if (placeholder.transform.GetSiblingIndex () < tempSI)
tempSI--;
break;
}
}
Vector3 tempPos = placeholderParent.GetChild (tempSI).transform.localPosition;
Vector3 tempPosPlace = placeholder.transform.localPosition;
Quaternion tempRot = placeholderParent.GetChild (tempSI).transform.localRotation;
Quaternion tempRotPlace = placeholder.transform.localRotation;
placeholderParent.GetChild (tempSI).transform.localPosition = tempPosPlace;
placeholderParent.GetChild (tempSI).transform.localRotation = tempRotPlace;
placeholder.transform.localPosition = tempPos;
placeholder.transform.localRotation = tempRot;
this.transform.localRotation = tempRot;
placeholder.transform.SetSiblingIndex (tempSI);
}
public void OnEndDrag (PointerEventData eventData)
{
this.transform.SetParent (parentToReturnTo);
this.transform.SetSiblingIndex (placeholder.transform.GetSiblingIndex ());
Vector3 temp = placeholder.transform.localPosition;
GetComponent<CanvasGroup> ().blocksRaycasts = true;
Destroy (placeholder);
this.transform.localPosition = temp;
}
}
Format pasted code with 101010 button. This has been done for you just this once; improper formatting is grounds for rejection.
Honestly, I wouldn't expect help proofreading a snippet this large / complex. Seems to me you could bypass this issue by letting index enforcement be a separate procedure that is based on the transform's position. Wherever possible, I avoid futzing with sibling order. I realize it's critical in UI stuff, but even then there's usually a way to take care of it that's simpler than trying to manage it directly alongside your manipulation logic.
Your answer

Follow this Question
Related Questions
Drag and Drop on RTS camera 1 Answer
Drag Item Scaling on Phone 0 Answers
How to move UI image between panels 0 Answers
How to find in what Scripts or components an Object is connected 0 Answers
Line Direction 1 Answer