ThisCalculator

Everyday

How to Build a Simple Game in Unity: Beginner Guide

Updated · Standard formula, shown below

How to Build a Simple Game in Unity: Beginner Guide

This guide walks you through creating your first playable game in Unity, from setting up a project to building a simple player-controlled cube that collects items. You’ll learn the core concepts of the Unity editor, GameObjects, components, and basic C# scripting. By the end, you’ll have a small game you can play and a solid foundation for making more complex projects.

What You’ll Need

  • A computer running Windows or macOS (Unity supports both).
  • The Unity Hub and a recent Unity version installed (the free Personal plan is fine).
  • Basic familiarity with the Unity editor interface (if you’re brand new, spend a few minutes clicking around first).
  • No prior programming experience is required, but a willingness to follow code examples is.

Step-by-Step

  1. Create a new project. Open Unity Hub, click “New Project,” choose the “3D Core” template, give it a name (e.g., “MyFirstGame”), and click “Create Project.” Unity will open the editor with a default scene containing a camera and a directional light.
  2. Set up the ground. In the Hierarchy window, right-click → 3D Object → Cube. Rename it “Ground.” In the Inspector, set its Position to (0, 0, 0) and Scale to (10, 1, 10). This creates a flat platform.
  3. Add a player object. Right-click in Hierarchy → 3D Object → Sphere. Rename it “Player.” Set its Position to (0, 1, 0) so it sits on the ground (the sphere’s radius is 0.5, so its center is 1 unit above the ground).
  4. Create a collectible item. Right-click → 3D Object → Cube. Rename it “Coin.” Set its Position to (2, 1, 2) and Scale to (0.5, 0.5, 0.5). Make it stand out by giving it a bright color: in the Inspector, click the material slot next to “Mesh Renderer” → the small circle icon → “Default-Material.” Instead, create a new material: in the Project window, right-click → Create → Material, name it “CoinMat,” set its Albedo color to yellow, and drag it onto the Coin object in the Scene view.
  5. Add a script to move the player. In the Project window, right-click → Create → C# Script, name it “PlayerMovement.” Double-click it to open in your code editor. Replace the default code with:
    using UnityEngine;
    
    public class PlayerMovement : MonoBehaviour
    {
        public float speed = 5f;
    
        void Update()
        {
            float moveX = Input.GetAxis("Horizontal");
            float moveZ = Input.GetAxis("Vertical");
            Vector3 movement = new Vector3(moveX, 0, moveZ) * speed * Time.deltaTime;
            transform.Translate(movement);
        }
    }
        

    Save the file and return to Unity. Drag the script from the Project window onto the Player object in the Hierarchy.

  6. Make the coin collectible. Create another C# script called “CoinCollect.” Open it and replace with:
    using UnityEngine;
    
    public class CoinCollect : MonoBehaviour
    {
        void OnTriggerEnter(Collider other)
        {
            if (other.CompareTag("Player"))
            {
                Destroy(gameObject);
            }
        }
    }
        

    Save and drag this script onto the Coin object.

  7. Set up the trigger. On the Coin object, in the Inspector, find the Box Collider component. Check the “Is Trigger” checkbox. This allows the OnTriggerEnter event to fire when the player overlaps the coin.
  8. Tag the player. Select the Player object. In the Inspector, at the top, click the “Tag” dropdown → “Add Tag…” → click the “+” and type “Player” → Save. Then select the Player object again, click the Tag dropdown, and choose “Player.” This is necessary for the coin script to recognize the player.
  9. Add a score counter (optional but fun). In the Hierarchy, right-click → UI → Text (Legacy) or TextMeshPro (if you prefer TMP). For simplicity, use Text (Legacy). Rename it “ScoreText.” In the Inspector, set its position via the Rect Transform (e.g., anchored top-left). Add a script to the Player called “ScoreManager” with a public int score = 0; and a method to add points. Then modify CoinCollect to call that method before destroying the coin.
  10. Test your game. Press the Play button at the top of the editor. Use the arrow keys or WASD to move the sphere. Drive it into the coin – the coin should disappear. If you added the score, it should update.
  11. Build the game. Go to File → Build Settings. Click “Add Open Scenes” to include your current scene. Click “Build” and choose a folder. Unity will create an executable you can run outside the editor.

Common Mistakes

  • Forgetting to save your script before returning to Unity. Unity won’t see changes until you save and recompile.
  • Not attaching the script to the correct GameObject. If the movement script is on the camera instead of the sphere, the sphere won’t move.
  • Using OnCollisionEnter instead of OnTriggerEnter. If you forget to check “Is Trigger,” the trigger event won’t fire.
  • Tag mismatch. If the player isn’t tagged exactly “Player” (case-sensitive), the coin won’t detect it.
  • Movement feels too fast/slow. This is often due to forgetting to multiply by Time.deltaTime, which makes movement frame-rate dependent.

Tips

  • Keep your scene organized: use folders in the Project window for scripts, materials, and prefabs.
  • Use prefabs for collectibles: create one coin, drag it into the Project window to make a prefab, then you can easily place multiple copies.
  • Start with simple shapes before worrying about 3D models.
  • Use Debug.Log() in your scripts to print messages to the Console for debugging.
  • Save your scene frequently (Ctrl+S or Cmd+S).

Troubleshooting

  • Player doesn’t move: Check that the PlayerMovement script is attached to the Player object. Ensure there are no compile errors (check Console window). Make sure you’re pressing the correct keys (arrow keys or WASD).
  • Coin doesn’t disappear: Verify the Coin has a Collider with “Is Trigger” checked, the CoinCollect script is attached, and the player is tagged “Player.” Also ensure the player has a Collider (it does by default).
  • Game view is black: Make sure your camera is positioned to see the scene. Select the camera and press Ctrl+Shift+F to align it to the current view.
  • Script errors: Read the error message in the Console. Often it’s a missing semicolon or a typo. Double-check that all variable names match.
  • Build fails: Check that you’ve added the scene to the Build Settings. Also ensure there are no errors in the Console.

Frequently Asked Questions

Do I need to know C# to make a game in Unity?

Not necessarily, but it helps. Unity uses C# for scripting, and even simple games require some code. You can start by copying examples and learning as you go.

Can I make a 2D game with this guide?

This guide is for 3D, but Unity supports 2D. You would use sprites instead of 3D objects and adjust the camera to orthographic. The scripting concepts are similar.

How do I add more levels or a win condition?

You can create multiple scenes and load them with SceneManager.LoadScene(). For a win condition, you could count coins and display a message when the count reaches a target.

Why is my player moving through the ground?

This happens if you’re using transform.Translate and the frame rate is low. For a simple game, you can add a Rigidbody component to the player and use physics-based movement (e.g., AddForce) to prevent falling through.

How do I make the game run on a mobile device?

You can switch the build target to Android or iOS in Build Settings. You’ll need to adjust input for touch (e.g., using Input.touches) and optimize performance.

Where can I find free assets for my game?

Unity Asset Store has many free assets. You can also use free 3D models from sites like Sketchfab (check licenses). For learning, stick with primitives.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.