Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by BusyRoots · Jan 18, 2018 at 11:18 AM · eventsystemdrag-and-drop

Variable gets overwritten when changing from OnDrag() to OnDrop()

Hi everyone,

I'am using the unity event system for my drag & drop functionality in my inventory. I experienced a change of the variable value between the "drag calls": OnBeginDrag(), OnDrag(), OnEndDrag() and the "drop call" OnDrop(). I created an example to show the problem:

  • created a scrpit 'TestDragDrop'

  • script is attached to two identical gameobjects Rect and Rect(1) which are placed in the scene, see: alt text

Inspector of Rect(1) (identical to Rect):

alt text

TestDragDrop(Script) - working principle:

  • In Start() I declare _testNumber = 3

  • OnBeginDrag() sets _testNumber = 5

  • during OnDrag() dragged gameobject (Rect or Rect(1) follows mouse cursor

  • OnDrop() gets called when dropping Rect on Rect(1) or vice versa

  • OnDrop() output says _testNumber = 3 <-- Why??? (i want it to be: _testNumber = 5)

  • output of OnEndDrag() says _testNumber = 5 <-- okay

I hope somebody can help me. If I forgot some information feel free to ask me :).

Here the complete script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 
 using UnityEngine.UI;
 
 public class TestDragDrop : MonoBehaviour, IBeginDragHandler, IDragHandler, IDropHandler, IEndDragHandler
 {
 
     private int _testNumber;
 
     // Use this for initialization
     void Start()
     {
         Debug.Log("Start");
         _testNumber = 3;
     }
 
     
     public void OnBeginDrag(PointerEventData eventData)
     {
         Debug.Log("OnBeginDrag");
         _testNumber = 5;
         Debug.Log("_testNumber = " + _testNumber);
 
         GetComponent<CanvasGroup>().blocksRaycasts = false;
         GetComponent<Image>().raycastTarget = false;
     }
 
 
     public void OnDrag(PointerEventData eventData)
     {
         Debug.Log("OnDrag");
         Debug.Log("_testNumber = " + _testNumber);
 
         // mouse Coordinates need to be converted to World coordinates
         var mouseCoordinates = Input.mousePosition;
         // use the inverted z-coordinate to make sure object gets displayed 
         // in front of the camera
         mouseCoordinates.z = -GameObject.Find("MainCamera").transform.position.z;
         transform.position = Camera.main.ScreenToWorldPoint(mouseCoordinates);
     }
 
 
     public void OnDrop(PointerEventData eventData)
     {
         Debug.Log("OnDrop");
         Debug.Log("_testNumber = " + _testNumber);
     }
 
 
     public void OnEndDrag(PointerEventData eventData)
     {
         Debug.Log("OnDragEnd");
         Debug.Log("_testNumber = " + _testNumber);
 
         GetComponent<CanvasGroup>().blocksRaycasts = true;
         GetComponent<Image>().raycastTarget = true;
     }
 
 
 }



rects-in-scene.jpg (13.9 kB)
gameobject-rect.jpg (54.2 kB)
Comment
Add comment · Show 1
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 BusyRoots · Jan 19, 2018 at 06:42 PM 0
Share

Here the console output: alt text

console-output.jpg (34.1 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by BusyRoots · Jan 19, 2018 at 06:50 PM

I figured it out. Actually everything is working how it should be. The mistake sat in front of the computer :D. Since the OnDrop() function gets called when you drop something an the game object (where OnDrop() is attached to) it gives me as output _testNumber = 3 because OnBeginDrag() hasn't been called on this game object. For a visual explanation see the attached image from my console output where I dropped the game object Rect on the game object Rect(1).

alt text


console-output-with-gameobject-names.jpg (45.7 kB)
Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

124 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 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

IDropHandler only trigger when it's SpriteRenderer sortingOder is higher than draggable sortingOder 0 Answers

transform.postion behaving strangely when dragging an object 0 Answers

How to render an image always on top? Already tried some solutions 0 Answers

How to Event tigger when drag and drop complete 1 Answer

IPointerExitHandler while holding click 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