Search Unity

Unity crashes when performing a drag/drop function

Discussion in 'Scripting' started by TheOtherUserName, Sep 18, 2021.

  1. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    I followed this tutorial on how to implement drag/drop but when trying to perform the drag and drop unity crashes.

    This is my script:
    Code (CSharp):
    1.     public void OnPointerDown(PointerEventData eventData)
    2.     {
    3.         Debug.Log("OnPointerDown");
    4.     }
    5.  
    6.     public void OnBeginDrag(PointerEventData eventData)
    7.     {
    8.         Debug.Log("OnBeginDrag");
    9.     }
    10.  
    11.     public void OnDrag(PointerEventData eventData)
    12.     {
    13.         rect.anchoredPosition += eventData.delta / canvas.scaleFactor;
    14.     }
    15.  
    16.     public void OnEndDrag(PointerEventData eventData)
    17.     {
    18.         Debug.Log("OnEndDrag");
    19.     }
    I have no compiler errors what so ever.
    BTW: this is my UI hirachy: Canvas/Inventory/Slots/Slot(This is the Object holding the file)
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Nothing in that script looks like it could cause the editor to crash, so I expect your problem is somewhere else.
     
    Bunny83 likes this.
  3. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    I thought so too but everything works except this. If I comment these lines out it doesn't crash also it only crashes if I try to use these features. Restarting Unity doesn't work either.

    I dont understand why it doesn't work since I have used it before.
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Conceivably, modifying anchoredPosition (line 13) could have some sort of interaction with some other code that causes a crash. If that were true, commenting out line 13 might avert the crash, even though it isn't primarily responsible.

    Though I am a bit suspicious of your test. If you commented out exactly and only the lines that you showed in your OP, I wouldn't expect the code to compile anymore, since you would have removed the implementation of some interfaces that presumably you named at the top of the class.