Using consistent naming conventions in Unity 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.
-
PascalCase for Classes:
- Use PascalCase for class names (e.g.,
PlayerController
,GameManager
). - Example:
public class PlayerController : MonoBehaviour { }
- Use PascalCase for class names (e.g.,
-
camelCase for Variables:
- Use camelCase for variables and private fields.
- Prefix private fields with
_
(e.g.,_playerHealth
,_isJumping
). - Example:
private int _playerHealth; public float movementSpeed;
-
CONSTANT_CASE for Constants:
- Use all-uppercase with underscores for constants.
- Example:
private const int MAX_HEALTH = 100;
-
k_Prefix for Static Variables:
- Prefix static fields with
k_
. - Example:
private static int k_MaxEnemies = 10;
- Prefix static fields with
-
Descriptive Names:
- Use clear and meaningful names to describe the purpose of a component or variable.
- Avoid generic names like
temp
,value
, orobject
.
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 |
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 |
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 |
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 |
-
Consistent Naming:
- Name objects based on their role and type.
- Avoid names like
GameObject1
orUntitled
.
-
Hierarchy Organisation:
- Group related GameObjects under empty parent GameObjects with descriptive names (e.g.,
Environment
,Enemies
,UI
).
- Group related GameObjects under empty parent GameObjects with descriptive names (e.g.,
-
Instance-Specific Names:
- Append instance identifiers when duplicating objects (e.g.,
Tree_01
,Tree_02
).
- Append instance identifiers when duplicating objects (e.g.,
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 |
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!");
}
}
- Abbreviations: Avoid unclear abbreviations. Use full words (e.g.,
HealthBar
instead ofHB
). - 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.
- A script to initialize a well-organized Unity project; works on Windows, macOS, and Linux
- 2024-12-13: The first version of this document was created.