logo
Published on

Unity 1. Basic Structure and the Input Error

Authors
  • avatar
    Name
    seren-wib
    Twitter
Contents

0. Introduction

When I first opened Unity, I could not see where folders, screens and code meet, so I wrote down the basic structure. Along the way an Input System setting blocked my movement code, so the fix is here too.

In restaurant terms, one open dining hall is the space opened at once, and each seat in it is something that exists there, guest or staff. You attach duties to each seat so it does work, and you save frequently used seat setups as cards. The instructions for those duties are written separately. The hall is a Scene, a seat is a GameObject, a duty is a Component, a saved card is a Prefab, and the instructions are a C# Script. This post covers the folder structure, Scene and GameObject, Component and Script, how to make backgrounds, and then fixes the Input error.

1. Project Folder Structure

The folders you work with in a Unity project are inside Assets.

Assets/
├─ Scenes/
├─ Scripts/
├─ Sprites/
└─ Prefabs/
  • Scenes: Scene files. One .unity file is one Scene.
  • Scripts: C# code files. A file does not run by itself; it runs only after being attached to a GameObject as a Component in the Unity Editor.
  • Sprites: 2D images, Sprite. Just an image, with no movement, collision or health. To show it on screen, the GameObject needs a SpriteRenderer.
  • Prefabs: a GameObject's configuration saved as a reusable source file. You create one by dragging the object into the Prefabs folder.

A Prefab does not merge scripts and images into one. It stores the list of attached Components, each setting, and the referenced Assets (player.png, PlayerMove.cs).

2. Project, Scene and GameObject

The hierarchy is Project > Scene > GameObject > Component. A Project is the whole game and holds Assets/, Packages/ and ProjectSettings/.

A Scene is a bundle of objects loaded at once. It is not a screen image but a file that stores which objects exist, where, and with which Components. The Camera in the Scene captures the picture and shows it in the Game View. A small game can put everything in one SampleScene; as it grows you split into MainMenu, GameScene and GameOver.

SampleScene
├─ Main Camera
├─ Player
├─ Enemy
└─ UI

An object like Player is not a standalone file; it is stored inside the Scene file (SampleScene.unity). That is why VS Code shows no Player.cs, only the code file PlayerMove.cs.

A GameObject is an empty shell and Components provide the functionality. The name Player is not required (Hero works too); what matters is which Components are attached.

3. Component Combinations

An object is a combination of Components. Player is configured like this.

ComponentRole
TransformPosition, rotation, scale. Every GameObject has one by default
@@spriterenderer@@SpriteRenderer@@/@@Displays a 2D image on screen
Rigidbody2DSubjects it to the 2D physics engine
BoxCollider2D (Collider)Collision area
PlayerMove.csMovement code written by hand

Building a feature always follows the same order: create a GameObject, attach Components and set values, write a C# Script, attach the Script to the GameObject, run and check.

4. How a Script Gets Attached

A class inheriting MonoBehaviour attaches to a GameObject as a Component.

using UnityEngine;

public class PlayerMove : MonoBehaviour
{
    public float speed = 5f;

    void Update()
    {
        float x = Input.GetAxisRaw("Horizontal");
        float y = Input.GetAxisRaw("Vertical");

        Vector3 direction = new Vector3(x, y, 0).normalized;
        transform.position += direction * speed * Time.deltaTime;
    }
}

Update() is called every frame. Multiplying by Time.deltaTime keeps the speed the same regardless of frame rate. transform in the code is the Transform of the GameObject the Script is attached to. It does not look objects up by name but works relative to its own GameObject, so attaching the same Script to Enemy makes Enemy move.

5. Roles of Unity Editor and VS Code

The @@unity-editor@@Unity Editor@@/@@ handles editing Scenes, creating GameObjects, adding Components, configuring Sprites, Prefabs, Colliders and Cameras, and test runs. VS Code handles C# Scripts and game logic.

6. Backgrounds and Structures

Not everything becomes a GameObject.

KindMethod
Things that move and actGameObject + Script
Floors, walls, backgrounds placed repeatedlyTilemap
Large backgrounds that are only an image@@spriterenderer@@SpriteRenderer@@/@@

Making 1000 floor tiles into 1000 GameObjects is wasteful, so under a Grid you put Ground Tilemap and Wall Tilemap, and one Tilemap manages the tile data. For 500 walls that means one Tilemap GameObject instead of Wall_001 to Wall_500.

7. Fixing the Input System Error

Running the code from section 4 failed on the line reading UnityEngine.Input with this error.

You are trying to read Input using the UnityEngine.Input class, but you have switched active Input handling to Input System package in Player Settings.

The cause: the project was set to use only the Input System package, so the old Input Manager API UnityEngine.Input was disabled. Setting Active Input Handling to Both in Player Settings enables both.

  1. Open Edit > Project Settings > Player.

    Player Settings screen
  2. Go to Other Settings.

    Other Settings screen
  3. Change the Active Input Handling value to Both.

    Active Input Handling settings screen

8. Reflection

At first you only need to understand this loop: create a GameObject, attach Components, write and attach a Script, reuse it as a Prefab, test it in the Scene.