0
\$\begingroup\$

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?

\$\endgroup\$
0
1
\$\begingroup\$

You're adding the LayoutElement referenced by the variable le to a new GameObject referenced by the variable placeholder

placeholder = new GameObject();
LayoutElement le = placeholder.AddComponent<LayoutElement>();

But you're then trying to read from a different LayoutElement attached to this script's GameObject:

 this.GetComponent<LayoutElement>().preferredWidth;

It's this second LayoutElement that is not being found. Have you verified you have such a LayoutElement on every instance that's running this dragable script?

You can use a [RequireComponent(typeof(LayoutElement)] attribute to enforce this dependency to help catch these errors at edit time.

\$\endgroup\$
1
  • \$\begingroup\$ It worked! I just forgot to put a LayoutElement on the cards, I think because in Unity 2017 it wasn't necessary to get them to be the right size (while it was in the tutorial which was with Unity 5 I believe) Thank you so much :) \$\endgroup\$ Feb 16 '18 at 1:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.