- Home /
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;
}
}
}
}
}
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.
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?
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
