I am trying to build a Hearthstone-style online TCG. Right now I am just working on getting the cards to drag around on the screen. Since I'm new to Unity, I'm using this tutorial https://www.youtube.com/watch?v=AM7wBz9azyU&index=3&list=PLbghT7MmckI42Gkp2cILkO2nRxK2M4NLo
I've copied the code in the tutorial pretty much exactly. What I have looks like this:
public class dragable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
public Transform parentToReturnTo = null;
GameObject placeholder = null;
public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log("OnBeginDrag");
placeholder = new GameObject();
placeholder.transform.SetParent(this.transform.parent);
LayoutElement le = placeholder.AddComponent<LayoutElement>();
if (le) {
Debug.Log("Found it!"); //Found le here...
}
else
{
Debug.Log("No dice.");
}
le.preferredWidth = this.GetComponent<LayoutElement>().preferredWidth; //can't find le--why?
le.preferredHeight = this.GetComponent<LayoutElement>().preferredHeight;
le.flexibleHeight = 0;
le.flexibleWidth = 0;
parentToReturnTo = this.transform.parent;
this.transform.SetParent(this.transform.parent.parent);
GetComponent<CanvasGroup>().blocksRaycasts = false;
}
The rest of the class doesn't seem to effect the problem I'm having. That problem being: whenever I try to write to the LayoutElement le, the console returns a NullReferenceException error. But when I try to read le--for instance, testing if it exists in the if (le) loop (which exists only for debugging this particular issue), it works fine. Also, if you watch the tutorial video, you'll see that there is a "tabletop" on the upper half of the canvas that you can drop cards on to--this doesn't seem to work unless I comment out all code that tries to write to le's attributes. Anyone know how to fix this?