Building a Merge Watermelon Game in Unity
Author: Shiguang (Yixian Studio)
Reviewed by: Baiyan
A game called Merge Watermelon recently swept through Qzone. After getting hooked, players jokingly began calling themselves “watermelon people.” Memes, parody images, and Watermelon bots soon followed, along with spin-offs such as Liver-Saving Merge Watermelon, Merge Tiny Grapes, and Merge a Great Nation… This article looks at how to build a Merge Watermelon game in Unity.
Step 1: Gathering assets
The assets, or textures, determine what the fruit looks like. Replace them with other elements—say, breasts (ahem), Polandballs, or something similar—and you can make parody versions such as Merge a Great Nation.
To keep things simple (actually, because I cannot draw), I drew a few balls to represent the fruit:

From left to right, they represent a grape, a little cutie, an orange, a lemon, a kiwifruit, a tomato, half a watermelon, and a whole watermelon. The rule is that two fruits of the same level merge into a fruit one level higher. You win once you create the watermelon.
I left out intermediate fruits such as coconuts and potatoes from the merge chain. The principle is the same, so there was no need to draw them~~ (All right, I was simply being lazy.~~)
Step 2: UI
I designed a simple UI:

The UI has three main parts. The first is the white image at the top, which displays the fruit that will appear next. The second is the score in the upper-left corner, which tracks the points earned by merging fruit. The third consists of the left, right, and bottom boundaries. Each has a collider to keep fruit from falling out of the screen.
With these two parts ready, we can start planning the program.
Step 3: Programming and implementation
The basic idea is to give every fruit a 2D rigidbody and a 2D circle collider, then instantiate the corresponding “next fruit” at the x-coordinate where the mouse is clicked. When two fruits collide, they are destroyed, a higher-level fruit is created, and the score increases.
First, we need to instantiate the next fruit at the mouse pointer’s x-coordinate. Because fruit objects will be destroyed, we cannot instantiate the original object directly; otherwise, once that source object is destroyed, we can no longer create new instances from it. The solution is to make a copy of the source object and perform all instantiation and destruction on those copies.

As the two images show, the objects marked with the public access modifier are the original fruit objects, while the implicitly declared objects below them are the corresponding copies. After a mouse click, the program uses the random number it has generated to select a fruit and instantiates it at the mouse pointer’s world-space x-coordinate. Here, 5.0f is a fixed y-coordinate, which makes the fruit fall from the top of the screen.
Next, we make two identical colliding fruits disappear, “merge” them into the next fruit, and award points. Here is the grape as an example:

Four things happen here: the collision Boolean is set to true, 10 points are added to the score, the collision position is recorded, and the two colliding objects are destroyed.
You may notice that I do not create the next-level object inside the collision event. Both colliding fruits have colliders, so this method is actually called twice, once by each fruit. If we instantiated the next-level fruit directly inside this method, we would get two fruits pressed together. They would merge immediately and rush straight to the end of the chain: the watermelon.
Instead, I use a Boolean flag. Another class checks it in LateUpdate and instantiates the next-level fruit there, which prevents two copies from being created:

The last step is to generate the random “next fruit.” This is straightforward: use a random number to choose one.

Conclu sion
Those are the basic ideas. This roughly 200-line remake of Merge Watermelon can already reproduce most of the original game’s features. It still lacks a loss condition (which could be implemented with a trigger plus timing in FixedUpdate (I was too lazy; besides, I never lose anyway)) and some “exciting” sound effects (heavy sarcasm), and it still does not feel quite as good as the original~

