Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
avatar image
0
Question by Nikolasio · Apr 21, 2016 at 11:38 AM · uilogicdrag and dropconditional

Conditionals in UI Drag and Drop?

Hi,
I have a nice UI drag and drop system (big thanks to quill18 and BoredMormon for their tutorials) and want to get some conditionals in it.
I have 4 numbers (that are instantiated on the canvas) that I can to drag and drop on 4 specific dropzones.
The numbers return to their original position if they're not dropped on the specific dropzone.

Here's the Drag script:

 public class Drag : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {    
     public Vector3             positionToReturnTo;
 
     public enum             Slot { ONE, TWO, THREE, FOUR};
     public Slot                typeOfItem = Slot.ONE;
 
     public void OnBeginDrag(PointerEventData eventData){
         positionToReturnTo = this.transform.position;
         GetComponent<CanvasGroup> ().blocksRaycasts = false;
         Debug.Log ("OnBeginDrag");
     }
 
     public void OnDrag(PointerEventData eventData){
         this.transform.position = eventData.position;
         Debug.Log ("OnDrag");
     }
 
     public void OnEndDrag(PointerEventData eventData){
         GetComponent<CanvasGroup> ().blocksRaycasts = true;
         this.transform.position = positionToReturnTo;
         Debug.Log ("OnEndDrag");
     }        
 }  

Here is the Dropzone script:

 public class Dropzone : MonoBehaviour, IDropHandler {
 
     public Drag.Slot     typeOfItem = Drag.Slot.ONE;
 
     public void OnDrop (PointerEventData eventData)
     {
         Debug.Log (eventData.pointerDrag.name + "was dropped on " + gameObject.name);
 
         Drag d = eventData.pointerDrag.GetComponent<Drag> ();
         if (d != null) {
             if (typeOfItem == d.typeOfItem) {
                 d.positionToReturnTo = this.transform.position;
             }        
 
         }
     }
 }

  

The logic I want to put in is:
- First numberOne needs to be placed on dropzoneOne
- Only then numberTwo can be placed on dropzoneTwo
- Only then numberThree can be placed on dropzoneThree
- Only then numberFour can be placed on dropzoneFour

I tried here below with bools but it doesn't work. Can anybody help me?
EDIT:
further information:

Only case “one” does work: numberTwo, numberThree and numberFour bounce back to their original positions before I drop numberOne on dropzoneOne. The bool oneInDropzone is also set to true.

After that: numberTwo, numberThree and numberFour keep bouncing back to their original positions when I drag and drop them. So it stops at case “two”, I can’t drop numberTwo on dropzoneTwo.

I made the bools public and in play mode I see that oneInDropzone = true in the Dropzone script on dropzoneOne, but I don’t see the change on dropZoneTwo,dropZoneThree,dropZoneFour. They all have the same script, how is that possible?

 public class Dropzone : MonoBehaviour, IDropHandler {
 
     public Drag.Slot     typeOfItem = Drag.Slot.ONE;
 
     bool                 oneInDropzone = false; 
     bool                 twoInDropzone = false; 
     bool                 threeInDropzone = false; 
 
 
     public void OnDrop (PointerEventData eventData) {
         Debug.Log (eventData.pointerDrag.name + "was dropped on " + gameObject.name);
 
         Drag d = eventData.pointerDrag.GetComponent<Drag> ();
         if (d != null) {
             if (typeOfItem == d.typeOfItem) {
                 switch (eventData.pointerDrag.tag) {
                 case "one":
                     d.positionToReturnTo = this.transform.position;
                     oneInDropzone = true;
                     break;
                 case "two":
                     if (oneInDropzone == true) {
                         d.positionToReturnTo = this.transform.position;
                         twoInDropzone = true;
                     }
                     break;
                 case "three":
                     if (twoInDropzone == true) {
                         d.positionToReturnTo = this.transform.position;
                         threeInDropzone = true;
                     }
                     break;
                 case "four":
                     if (threeInDropzone == true) {
                         d.positionToReturnTo = this.transform.position;
                     }
                     break;
                 }
             }
                 
         }
 
     }
 
 }

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 Layas · Apr 04, 2017 at 12:59 PM 0
Share

Hi, I have the same requirement, did you solve this?

1 Reply

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

Answer by Soraphis · Apr 21, 2016 at 12:13 PM

the problem is: you have seperate drop zones for the different dragables. setting the boolean for "ONE" to true, for one dropzone does not set it to true for each dropzone

a quick solution should be to make the boolean variables static.

i would not recommend this solution, btw. I'd change the drag-drop system to fit better into my game.

Comment
Add comment · Show 2 · 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 Nikolasio · Apr 21, 2016 at 02:37 PM 0
Share

Thank you for your answer, that is indeed the problem. I used private static boolean variables and the conditionals work, thank you very much. I understand that a static variable is a variable of the class itself, not the instances of the class, but, as I'm a beginner, why not using statics in this case? What would be than a proper solution, how would you change the drag and drop system?

avatar image Nikolasio · Apr 28, 2016 at 06:51 AM 0
Share

For me the question is answered. I also posted the question on another forum: link text.
The suggestion there was to keep the references of the instances of the Dropzone script in an Array and then enable/disable them according to what is necessary.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do you spawn objects from UI ScrollView via drag? 0 Answers

Click an object to show its group / structure / tree ? 0 Answers

How to do a raycast between an UI Element and Sprite 2D? 0 Answers

Understanding UI Logic 0 Answers

Question on Tutorial: Panes, Panels, Windows 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