Drag-and-drop match game system

Suleiman Abdullah
3 min readSep 9, 2023

--

Objective: Drag-and-drop match game system.

This is the continuous article from the last article,

Here is a link from the previous article .https://medium.com/@suleimanabdullah/making-a-drag-and-drop-item-system-61abd004cfdd

In the first step, I would add a text component in the ItemSlot, and give the name of the item that needs to be dropped into that slot.

In the next step let's rename our item name to exactly like its context image.

Open the Item script, create a public Vector3 variable called _oldPosition, and set it in value at the start as the Item Image. rectTransform.localPosition.

Create a method Called ResetPosition.This method is responsible for resetting the item position, we do this by assigning an image.RectTransform.localPosition equal to _oldPosition.

We call this method when we let go dragged item, We call the ResetPosition OnEndDrag method.

Go to the ItemSlot variable and create a string variable, which will be serialized, then go to the inspector and rename this instance on each slot according to its slot title.

Now go to the Ondrop method, in slot script, You need to read the previous article to understand this the link is at the top of this article. Here I will delete the last logic OnDrop method then I will add an if condition to check if eventData.pointerDrag.name is equal to _slotName.

Create the image component and cache this component in the start method.

Go to the OnDrop method in the if condition, create the Item script local variable then assign it to the eventData.pointerDrag to GetComponent of Item. Then set Item’s _old position to localPosition of _slotImage.

This is how I created the Drag-and-drop match game system.

See you in the next one.

--

--

Suleiman Abdullah

Self taught Unity Developer, who is passion about making games ,and he loves Math For Game Development