How to Create a Brain Test–Style Puzzle Game in Unity (Step-by-Step Guide for Beginners)


I will guide you step-by-step through the process of setting up Unity and creating your first "Brain Test" style puzzle.

We'll start with the absolute basics and build from there. I'll provide code snippets, explanations, and instructions on how to interact with the Unity Editor.

Part 2 Building Your First Brain Test Puzzle Logic in Unity (With Full Code)


Let's Begin: Unity Setup and Your First Interactable Object

Goal for this step:

  1. Install Unity and create a new 2D project.

  2. Set up the basic UI Canvas for your puzzle.

  3. Create a simple image that the player can tap.

  4. Write a script to detect taps on that image.


Step 1: Install Unity and Create a New Project

  1. Download Unity Hub:

    • Go to the official Unity website: https://unity.com/download

    • Click "Download Unity Hub" and install it on your computer.

  2. Install a Unity Editor Version:

    • Open Unity Hub.

    • Go to the "Installs" tab on the left.

    • Click "Install Editor."

    • I recommend installing the latest LTS (Long Term Support) version (e.g., Unity 2022.3.x LTS). LTS versions are more stable and supported for longer. Make sure "Android Build Support" and "iOS Build Support" are checked under the modules, as your GDD specifies these platforms.

    • Click "Install." This might take a while.

  3. Create a New Unity Project:

    • Once an Editor version is installed, go to the "Projects" tab in Unity Hub.

    • Click the "New Project" button.

    • In the templates list, select "2D Core".

    • Under "Project Name," type: TrickyLogicPuzzle

    • Choose a convenient "Location" on your computer.

    • Click "Create project."

    Unity will now open, and it might take a few minutes to import all the necessary assets. Be patient!


Step 2: Basic Project Structure and UI Canvas Setup

Once your project opens, you'll see the Unity Editor. Let's organize things and set up your main screen.

  1. Project Organization (Folders):

    • In the Project window (usually at the bottom), right-click on the Assets folder.

    • Select Create > Folder.

    • Name the new folder _Scenes.

    • Repeat this process to create the following folders: _Scripts_Sprites_UI_Audio_Prefabs. (The underscore _ helps them sort to the top).

  2. Save Your Current Scene:

    • Go to File > Save As...

    • Navigate into your newly created _Scenes folder.

    • Name the scene GameScene and click Save.

  3. Set Up the UI Canvas:

    • In the Hierarchy window (usually on the left), right-click anywhere.

    • Select UI > Canvas.

    • This will create three new objects: CanvasEventSystem.

  4. Configure the Canvas:

    Your Game view (usually in the middle) should now show a canvas that adapts to different screen sizes.


Step 3: Add Text for the Puzzle Question

  1. Install TextMeshPro Essentials:

    • Go to Window > TextMeshPro > Import TMP Essential Resources.

    • Click Import in the pop-up window. This replaces Unity's default UI text with a much better text rendering system.

  2. Add Puzzle Question Text:

    • In the Hierarchy, right-click on your Canvas object.

    • Select UI > Text - TextMeshPro. This will create a New Text (TMP) object as a child of Canvas.

    • Select New Text (TMP) in the Hierarchy.

    • In the Inspector, rename it to QuestionText.

    • In the Rect Transform component, set:

      • Pos X0

      • Pos Y800 (This puts it near the top of the screen)

      • Width900

      • Height200

      • You can drag the blue square handles in the Scene view to resize it if needed.

    • In the TextMeshPro - Text component, for the Text Input field:

      • Type: What object is out of place? (This will be our first simple puzzle question).

      • Set Font Size to 60.

      • Click the Align buttons to set Center for both horizontal and vertical alignment.


Step 4: Create a Simple Image for Interaction

Let's add a few objects that the player can tap. For now, we'll use Unity's default sprites.

  1. Add a Square Image:

    • In the Hierarchy, right-click on your Canvas.

    • Select UI > Image.

    • Rename this Image to SquareObject.

    • Select SquareObject. In the Inspector:

      • Rect Transform > Pos X-200

      • Rect Transform > Pos Y0

      • Rect Transform > Width200

      • Rect Transform > Height200

      • Image (Script) > Color: Click the color picker and choose a distinct color (e.g., Red).

    • Make sure "Raycast Target" is checked on the Image component. This allows it to receive touch input.

  2. Add a Circle Image:

    • In the Hierarchy, right-click on your Canvas.

    • Select UI > Image.

    • Rename this Image to CircleObject.

    • Select CircleObject. In the Inspector:

      • Rect Transform > Pos X200

      • Rect Transform > Pos Y0

      • Rect Transform > Width200

      • Rect Transform > Height200

      • Image (Script) > Color: Click the color picker and choose a distinct color (e.g., Blue).

      • Click the small circle next to the Source Image field. In the popup, search for Circle and select UI/Skin/Knob. This will make it a circular image.

    • Make sure "Raycast Target" is checked.

  3. Save Your Scene: File > Save (or Ctrl+S / Cmd+S).

    You should now see your question text, a red square, and a blue circle in your Game view.


Step 5: Write the Tap Detection Script

Now for the C# code! We'll make a script that can be attached to any UI Image to make it tap-able.

  1. Create a Script:

    • In the Project window, navigate to your _Scripts folder.

    • Right-click in the _Scripts folder.

    • Select Create > C# Script.

    • Name it TapInteractableMake sure the name is exactly 

  2. Open the Script:

    • Double-click TapInteractable in the Project window. This will open it in your code editor (Visual Studio by default).

  3. Paste the Code:

    • Delete all the default code in the file.

    • Paste the following code into TapInteractable.cs:

    C#
    using UnityEngine;
    using UnityEngine.UI; // Needed for Image component
    using UnityEngine.EventSystems; // Needed for event interfaces
    
    public class TapInteractable : MonoBehaviour, IPointerClickHandler
    {
        // This is a special Unity event function that gets called when the UI element is clicked
        public void OnPointerClick(PointerEventData eventData)
        {
            Debug.Log(gameObject.name + " was tapped!");
    
            // Example: Change color when tapped
            Image image = GetComponent<Image>();
            if (image != null)
            {
                image.color = Color.green; // Turn green when tapped
            }
        }
    }
  4. Save the Script: File > Save (or Ctrl+S / Cmd+S) in your code editor.


Step 6: Attach the Script to Your Objects

  1. Attach to SquareObject:

    • Go back to Unity Editor.

    • In the Hierarchy, select SquareObject.

    • In the Inspector, click Add Component.

    • Search for TapInteractable and select it.

  2. Attach to CircleObject:

    • In the Hierarchy, select CircleObject.

    • In the Inspector, click Add Component.

    • Search for TapInteractable and select it.

  3. Save Your Scene: File > Save (or Ctrl+S / Cmd+S).


Step 7: Test Your First Interaction!

  1. Run the Game:

    • Click the Play button (the triangle) at the top center of the Unity Editor.

    • The Game view will become active.

  2. Tap the Objects:

    • Click on the red square in the Game view.

    • Look at the Console window (usually at the bottom, next to Project). You should see: SquareObject was tapped!

    • The SquareObject should also turn green.

    • Click on the blue circle.

    • You should see: CircleObject was tapped!

    • The CircleObject should also turn green.

  3. Stop the Game:

    • Click the Play button again to stop the game.

    • Notice that the colors will revert to their original state because changes made in Play Mode are temporary.


Congratulations! You've just set up your Unity project, created a basic UI, and implemented your first interactive element with C# scripting. This is a huge first step for a "Brain Test" game.

The TapInteractable script is very generic right now. In the next steps, we'll expand it to make some objects "correct" and others "incorrect" for a specific puzzle, and then we'll add the game manager logic.

Let me know if you encountered any issues or if anything was unclear

Part 2 Building Your First Brain Test Puzzle Logic in Unity (With Full Code)

Comments

Popular posts from this blog

Step-by-Step Guide on How to Create a GDD (Game Design Document)

Master Drag & Drop in Unity: Build Engaging Puzzle Games (Step-by-Step Tutorial)