I build a brand new cards game every thing is work with out any errors , But wen i start to drag my object in this case is a card in to the drop zone my card is not visible for some reason , i am not sure but i thing they out of screen range or some thing like that i can not find the way to correct this , i try to debug but there is no error on it so i am not able to find the way to solve this.
I have try to add the object and set it to mouse position but i still not see the object is not visible
This is my code for the draggable object
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.EventSystems;
public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
private Vector3 mouseposition;
private Rigidbody rd;
private Vector2 direction;
private float moveSpeed = 100f;
public Transform parentToReturnTo = null;
public Transform placeholderParent = null;
public Transform card1xx1;
public Transform card1xx2;
public Transform card1xx3;
public Transform card1xx4;
GameObject placeholder = null;
void Start()
{
rd = GetComponent<Rigidbody> ();
}
public void OnBeginDrag(PointerEventData eventData) {
Debug.Log ("OnBeginDrag");
placeholder = new GameObject();
placeholder.transform.SetParent( this.transform.parent );
LayoutElement le = placeholder.AddComponent<LayoutElement>();
le.preferredWidth = this.GetComponent<LayoutElement>().preferredWidth;
le.preferredHeight = this.GetComponent<LayoutElement>().preferredHeight;
le.flexibleWidth = 0;
le.flexibleHeight = 0;
le.transform.position = Input.mousePosition;
placeholder.transform.SetSiblingIndex( this.transform.GetSiblingIndex() );
parentToReturnTo = this.transform.parent;
placeholderParent = parentToReturnTo;
this.transform.SetParent( this.transform.parent.parent );
GetComponent<CanvasGroup>().blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData)
{
//Debug.Log ("OnDrag");
Vector3 mouse = Input.mousePosition;
card1xx1.transform.position = mouse;
this.transform.position = eventData.position;
if(placeholder.transform.parent != placeholderParent)
placeholder.transform.SetParent(placeholderParent);
int newSiblingIndex = placeholderParent.childCount;
for(int i=0; i < placeholderParent.childCount; i++) {
if(this.transform.position.x < placeholderParent.GetChild(i).position.x) {
newSiblingIndex = i;
if(placeholder.transform.GetSiblingIndex() < newSiblingIndex)
newSiblingIndex--;
break;
}
}
placeholder.transform.SetSiblingIndex(newSiblingIndex);
}
public void OnEndDrag(PointerEventData eventData) {
Debug.Log ("OnEndDrag");
this.transform.SetParent( parentToReturnTo );
this.transform.SetSiblingIndex( placeholder.transform.GetSiblingIndex() );
GetComponent<CanvasGroup>().blocksRaycasts = true;
Destroy(placeholder);
}
}