• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Mar 20, 2017 at 09:30 PM by ZachRoman121 for the following reason:

Other

avatar image
Question by ZachRoman121 · Mar 20, 2017 at 07:06 PM · drag-and-dropinventory systemitems

Call Pointer Enter through GameObject

I am trying to make a drag and drop system for my inventory. But. The slots are underneath the items. And the Pointer Enter trigger is on the slots. So. Pointer Enter isn't being called. How do i fix t$$anonymous$$s? Here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class slotDragDetection : MonoBehaviour {
 
     public int Slot;
     bool pointerEntered = false;
     bool pointerClicked = false;
 
 
 
     void Awake() {
         EventTrigger trigger = gameObject.AddComponent<EventTrigger> ();
         EventTrigger.Entry entry = new EventTrigger.Entry ();
         EventTrigger.Entry entry2 = new EventTrigger.Entry ();
         EventTrigger.Entry entry3 = new EventTrigger.Entry ();
         EventTrigger.Entry entry4 = new EventTrigger.Entry ();
         //Entry one
         entry.eventID = EventTriggerType.PointerEnter;
         entry.callback.AddListener( ( data ) => { onPointerEnter( (PointerEventData)data ); } );
         trigger.triggers.Add( entry );
         //Entry two
         entry2.eventID = EventTriggerType.PointerExit;
         entry2.callback.AddListener( ( data ) => { onPointerExit( (PointerEventData)data ); } );
         trigger.triggers.Add (entry2);
         //Entry three
         entry3.eventID = EventTriggerType.PointerDown;
         entry3.callback.AddListener( ( data ) => { onPointerDown( (PointerEventData)data ); } );
         trigger.triggers.Add (entry3);
         //Entry four
         entry4.eventID = EventTriggerType.PointerUp;
         entry3.callback.AddListener( ( data ) => { onPointerUp( (PointerEventData)data ); } );
         trigger.triggers.Add (entry4);
     }
         
 
     public void onPointerEnter(PointerEventData data) {
         pointerEntered = true;
         Debug.Log ("Entered");
     }
 
     public void onPointerExit(PointerEventData data) {
         pointerEntered = false;
         Debug.Log ("Exited");
     }
 
     public void onPointerDown(PointerEventData data) {
         pointerClicked = true;
     }
 
     public void onPointerUp(PointerEventData data) {
         pointerClicked = false;
     }
 
 }
 

Edit: Here's my 2nd script for dragging the items: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems;

 public class dragItems : MonoBehaviour {
 
     Transform defaultSlot;
 
     float offsetX;
     float offsetY;
 
 
     void Awake() {
         EventTrigger trigger = gameObject.AddComponent<EventTrigger> ();
         EventTrigger.Entry entry = new EventTrigger.Entry ();
         EventTrigger.Entry entry2 = new EventTrigger.Entry ();
         EventTrigger.Entry entry3 = new EventTrigger.Entry ();
         //Entry one
         entry.eventID = EventTriggerType.BeginDrag;
         entry.callback.AddListener( ( data ) => { onBeginDrag( (PointerEventData)data ); } );
         trigger.triggers.Add( entry );
         //Entry two
         entry2.eventID = EventTriggerType.Drag;
         entry2.callback.AddListener( ( data ) => { onDrag( (PointerEventData)data ); } );
         trigger.triggers.Add (entry2);
         //Entry three
         entry3.eventID = EventTriggerType.EndDrag;
         entry3.callback.AddListener( ( data ) => { onEndDrag( (PointerEventData)data ); } );
         trigger.triggers.Add (entry3);
     }
     public void onBeginDrag( PointerEventData data) {
         offsetX = transform.position.x - Input.mousePosition.x;
         offsetY = transform.position.y - Input.mousePosition.y;
         if (transform.parent.parent.parent != null && transform.parent.parent.parent.CompareTag ("inventory")) {
             defaultSlot = transform.parent.transform;
             transform.SetParent (transform.parent.parent.parent);
         }
     }
     public void onDrag(PointerEventData data) {
         transform.position = new Vector3 (offsetX + Input.mousePosition.x, offsetY + Input.mousePosition.y);
     }
 
     public void onEndDrag(PointerEventData data) {
             transform.position = defaultSlot.transform.position;
             transform.SetParent (defaultSlot);
     }
 }
 
Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image

Answer by Feyyyyy · Mar 20, 2017 at 07:21 PM

Hi,

If I understad you corectly, there should be an interactable option on your items. Uncheck them. So your slots will start detecting mouse events instead of items.

Edit According to your comment, in your item class you ca do somet$$anonymous$$ng like that;

     public class ItemUI {
         public slotDragDetection currentSlot;
     
         public void onPointerEnter(PointerEventData data) {
              currentSlot.onPointerEnter(data);
          }
      
          public void onPointerExit(PointerEventData data) {
              currentSlot.onPointerExit(data);
          }
      
          public void onPointerDown(PointerEventData data) {
              currentSlot.onPointerDown(data);
          }
      
          public void onPointerUp(PointerEventData data) {
              currentSlot.onPointerUp(data);
          }
     
     }

Now your slots aren't exactly receiving the events but t$$anonymous$$s can work for your situation. I can't be sure that it will work for you, since I do not know your needs exactly.

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ZachRoman121 · Mar 20, 2017 at 07:23 PM 0
Share

But. I need to drag the items aswell as make the slots detect the cursor

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

65 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I need to find inventory and item system, and to save them 0 Answers

Decorator pattern for inventory item class 0 Answers

Is it good practice to keep a reference to an GO to avoid having to instantiate it? 1 Answer

Inventory armor wielding proplem,How to convert from derived to base 1 Answer

Override method for items in xml list? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges