Search Unity

Drop script not working...

Discussion in 'Scripting' started by heyramb89, Apr 19, 2019.

  1. heyramb89

    heyramb89

    Joined:
    Dec 29, 2018
    Posts:
    29
    Hey,

    I have a game with inventory and want some items to be dropped on other items to interact with them.
    E.g. drag-dropping a key on chest to open it.
    It was working good when everything was in canvas but for some reason I have to move other thins out of the canvas and kept only UI in there.
    But now my drop script is not showing anything.
    Perhaps because the chest has sprite renderer which is without raycast hit.
    So, my question is, how should I make it work?

    Also, inventory slot has drophandler eventsystem script attached, canvas render mode is Screen Space - Overlay, just for information.
    Please help, I don't know much about Unity & C#.
    The code is pasted below.

    Code (CSharp):
    1. ........
    2.  
    3. public void OnDrop(PointerEventData data)
    4.     {
    5.         if (data.pointerDrag != null)
    6.         {
    7.             RaycastHit hit;
    8.             var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    9.  
    10.             if (Physics.Raycast(ray, out hit))
    11.             {
    12.                 if (hit.collider != null)
    13.                 {
    14.                     Destroy(gameObject);
    15.                     ItemUsed();
    16.                 }
    17.             }
    18.         }
    19.     }
    20.  
    21.     public virtual void ItemUsed()
    22.     {    ............
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    I'm not familiar with that IDraggable / IDroppable (or whatever it is) interface. It's possible it will only work in the Canvas / EventSystem context.

    One idea is you might need to keep an invisible UI system around and proxy drop events to and from it over to other GameObjects that are controlling sprites in the same location.
     
    heyramb89 likes this.
  3. heyramb89

    heyramb89

    Joined:
    Dec 29, 2018
    Posts:
    29