# Unity Naming Conventions Using consistent naming conventions in [Unity](https://unity.com) projects helps keep everything organized and easy to work with. Below is a list of recommended conventions for naming components, scripts, variables, methods, folders, and game objects in a Unity project. --- ## General Rules 1. **PascalCase for Classes**: - Use **PascalCase** for class names (e.g., `PlayerController`, `GameManager`). - Example: ```csharp public class PlayerController : MonoBehaviour { } ``` 2. **camelCase for Variables**: - Use **camelCase** for variables and private fields. - Prefix private fields with `_` (e.g., `_playerHealth`, `_isJumping`). - Example: ```csharp private int _playerHealth; public float movementSpeed; ``` 3. **CONSTANT_CASE for Constants**: - Use all-uppercase with underscores for constants. - Example: ```csharp private const int MAX_HEALTH = 100; ``` 4. **k_Prefix for Static Variables**: - Prefix static fields with `k_`. - Example: ```csharp private static int k_MaxEnemies = 10; ``` 5. **Descriptive Names**: - Use clear and meaningful names to describe the purpose of a component or variable. - Avoid generic names like `temp`, `value`, or `object`. --- ## Scripts and Components | **Category** | **Convention** | **Example** | |-------------------------|------------------------------------|-------------------------| | Manager Classes | Suffix with `Manager` | `GameManager`, `UIManager` | | Player-Specific Classes | Prefix with `Player` | `PlayerController`, `PlayerStats` | | Singleton Classes | Suffix with `Instance` | `AudioManagerInstance` | | Physics-Related Classes | Suffix with `Physics` | `RigidbodyPhysics`, `ProjectilePhysics` | | UI Components | Prefix with `UI` | `UIButton`, `UIHealthBar` | | System Helpers | Suffix with `Helper` or `Utility` | `MathHelper`, `InputUtility` | --- ## Variables | **Type** | **Convention** | **Example** | |-------------------|-----------------------------|------------------------| | Private Fields | `camelCase` with `_` prefix | `_playerScore`, `_isPaused` | | Public Fields | `camelCase` | `movementSpeed`, `health` | | Properties | `PascalCase` | `PlayerName`, `IsAlive` | | Constants | `CONSTANT_CASE` | `MAX_HEALTH`, `GRAVITY_FORCE` | --- ## Folders Organise folders with a naming structure that reflects their purpose: | **Folder** | **Convention** | **Example** | |-------------------|-------------------------------|--------------------------| | Scripts | `Scripts` or `[Feature]/Scripts` | `Player/Scripts`, `UI/Scripts` | | Prefabs | `Prefabs` | `Player/Prefabs`, `Environment/Prefabs` | | Materials | `Materials` | `Player/Materials`, `UI/Materials` | | Animations | `Animations` | `Player/Animations` | | Audio | `Audio` | `Audio/Music`, `Audio/SFX` | --- ## GameObjects | **Category** | **Convention** | **Example** | |--------------------|----------------------------------|----------------------------| | Root Objects | Descriptive PascalCase | `PlayerCharacter`, `MainCamera` | | UI Elements | Prefix with `UI` | `UICanvas`, `UIHealthBar` | | Lights | Suffix with `Light` | `DirectionalLight`, `Spotlight` | | Environment Objects| Descriptive PascalCase | `Tree`, `RockLarge`, `HouseSmall` | | Enemy Objects | Prefix with `Enemy` | `EnemyGoblin`, `EnemyBoss` | | Spawners | Suffix with `Spawner` | `EnemySpawner`, `ItemSpawner` | ### Additional Rules for GameObjects: 1. **Consistent Naming**: - Name objects based on their role and type. - Avoid names like `GameObject1` or `Untitled`. 2. **Hierarchy Organisation**: - Group related GameObjects under empty parent GameObjects with descriptive names (e.g., `Environment`, `Enemies`, `UI`). 3. **Instance-Specific Names**: - Append instance identifiers when duplicating objects (e.g., `Tree_01`, `Tree_02`). --- ## Public Methods | **Action** | **Convention** | **Example** | |-------------------|-------------------------------|--------------------------| | Actions | Use verbs in `PascalCase` | `MovePlayer`, `AttackEnemy` | | Getters/Setters | Prefix with `Get` or `Set` | `GetHealth`, `SetName` | | Events | Use `On` Prefix | `OnPlayerDeath`, `OnButtonClicked` | --- ## Example Component ```csharp using UnityEngine; public class PlayerController : MonoBehaviour { // Private fields private float _speed = 5f; private int _health = 100; // Public properties public int Health { get { return _health; } set { _health = Mathf.Clamp(value, 0, 100); } } // Constants private const int MAX_ENEMIES = 10; // Methods private void MovePlayer() { float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); Vector3 direction = new Vector3(horizontal, 0, vertical); transform.Translate(direction * _speed * Time.deltaTime); } private void OnPlayerDeath() { Debug.Log("Player has died!"); } } ``` --- ## Additional Tips - **Abbreviations**: Avoid unclear abbreviations. Use full words (e.g., `HealthBar` instead of `HB`). - **Hierarchy Naming**: Name GameObjects with descriptive names to match their role (e.g., `PlayerCharacter`, `EnemySpawner`). - **Grouping**: Use folders in the project and GameObject hierarchy to keep related components together. --- ## Additional Resources - [A script to initialize a well-organized Unity project](https://gist.github.com/habedi/c6bbe3cd3351c32c592a19314cf34e14); works on Windows, macOS, and Linux ## Changelog - 2024-12-13: The first version of this document was created.