Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Inventory system not working

Discussion in 'UGUI & TextMesh Pro' started by MatheusMarkies, Oct 17, 2021.

  1. MatheusMarkies

    MatheusMarkies

    Joined:
    Apr 16, 2017
    Posts:
    67
    Very late I'm creating an inventory system and to move items through it I use an OnPointerEnter and then a movetowards.
    However, when I try to identify if the mouse is over one of the items, it returns as if it were on top of them all.



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class ItemUI : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
    7. {
    8.     public bool dragging;
    9.     public RPGApplication rpgApplication;
    10.     public bool onMouseOver = false;
    11.     public int itemIndex;
    12.  
    13.     private void Start()
    14.     {
    15.         rpgApplication = GameObject.FindGameObjectWithTag("Management").GetComponent<RPGApplication>();
    16.     }
    17.  
    18.     public void Update()
    19.     {
    20.         if (Input.GetButtonDown("Fire1") && onMouseOver)
    21.             dragging = !dragging;
    22.         else if (Input.GetButtonDown("Fire1") && dragging)
    23.             dragging = false;
    24.  
    25.             if (dragging)
    26.         {
    27.             Vector2 nearTile = Vector2.zero;
    28.             foreach (Vector2 vec in rpgApplication.rpgView.playerInventory.inventoryTiles)
    29.                 if (Vector2.Distance(new Vector2(Input.mousePosition.x, Input.mousePosition.y), vec) < Vector2.Distance(new Vector2(Input.mousePosition.x, Input.mousePosition.y), nearTile))
    30.                     nearTile = vec;
    31.             transform.position = Vector2.MoveTowards(transform.position, nearTile, 600 * Time.deltaTime);// new Vector2(Input.mousePosition.x, Input.mousePosition.y);
    32.             rpgApplication.rpgView.playerInventory.itemsList[itemIndex].currentTile = nearTile;
    33.         }
    34.     }
    35.     public void OnPointerEnter(PointerEventData eventData)
    36.     {
    37.         onMouseOver = true;
    38.     }
    39.  
    40.     public void OnPointerExit(PointerEventData eventData)
    41.     {
    42.         onMouseOver = false;
    43.     }
    44. }
    45.  
     
  2. andrew-lukasik

    andrew-lukasik

    Joined:
    Jan 31, 2013
    Posts:
    230
  3. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    655
    I moved the thread to the correct Forum