Hi, I have a problem with unity, I have my code for drag and drop, and I have a function called dragged, but when I call it, nothing happens in unity, for example, I have a variable that is dragged, in the console is wrote that boolean is true, but in unity it is not, and I don’t know why this is happening.
My code:
HoverButton:
public class HoverButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
public InventorySlot hoverButton;
public void OnPointerEnter(PointerEventData eventData)
{
InventorySlot comp = eventData.pointerCurrentRaycast.gameObject.transform.parent.GetComponent<InventorySlot>();
if (comp == null)
comp = eventData.pointerCurrentRaycast.gameObject.transform.parent.parent.GetComponent<InventorySlot>();
if (comp != null)
{
hoverButton = comp;
}
}
public void OnPointerExit(PointerEventData eventData)
{
hoverButton = null;
}
private void Update()
{
if (hoverButton && !Inventory.instance.dadrop.isDragging)
{
if (hoverButton.item == null) return;
if (!Input.GetKeyDown(KeyCode.Mouse1)) return;
Inventory.instance.dadrop.dragItem(hoverButton, hoverButton.item);
hoverButton.ClearSlot();
}
}
}
DragAndDrop.cs:
public class DragAndDrop : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
public Image dadicon;
public bool isDragging;
public InventorySlot oldButton;
Item oldItem;
public InventorySlot hoverButton;
public void OnPointerEnter(PointerEventData eventData)
{
InventorySlot comp = eventData.pointerCurrentRaycast.gameObject.transform.parent.GetComponent<InventorySlot>();
if (comp == null)
comp = eventData.pointerCurrentRaycast.gameObject.transform.parent.parent.GetComponent<InventorySlot>();
if (comp != null)
{
hoverButton = comp;
}
}
public void OnPointerExit(PointerEventData eventData)
{
hoverButton = null;
}
public void dragItem(InventorySlot _oldButton, Item _oldItem)
{
oldButton = _oldButton;
oldItem = _oldItem;
dadicon.gameObject.SetActive(true);
dadicon.sprite = oldButton.icon.sprite;
isDragging = true;
Debug.Log(isDragging);
}
public void dropItem()
{
if (hoverButton != null)
{
if (hoverButton.icon.sprite)
{
oldButton.AddItem(hoverButton.item);
hoverButton.ClearSlot();
hoverButton.AddItem(oldItem);
}
else
{
hoverButton.AddItem(oldItem);
}
dadicon.gameObject.SetActive(false);
oldButton = null;
oldItem = null;
isDragging = false;
}
}
private void Update()
{
if (oldButton != null)
{
Vector2 mousepos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
dadicon.transform.position = Input.mousePosition;
if (Input.GetKeyDown(KeyCode.Mouse1) || Input.GetKeyDown(KeyCode.Mouse0))
{
dropItem();
}
}
}
}
Unity:

Here is true

But here isn’t
![]()
This should be active
![]()
But isn’t
I have one idea, I accidentally create a second instance of draganddrop.cs somewhere, but I don’t know where