C# for Unity and Game Development - Complete Notes
1. Introduction to C# in Unity
C# is the primary scripting language used in Unity for developing gameplay, game mechanics, AI, UI logic,
etc.
Unity uses Mono or IL2CPP to compile C# scripts.
Key Concepts:
- Scripts must inherit from MonoBehaviour.
- Common Unity methods: Start(), Update(), FixedUpdate(), LateUpdate()
- Scripts must be attached to GameObjects to run.
2. Variables and Data Types
- int, float, bool, string, char
- Unity-specific types: Vector2, Vector3, Quaternion, Transform, GameObject
Example:
public int health = 100;
public GameObject player;
3. Functions and Methods
- Define with return type and parameters.
C# for Unity and Game Development - Complete Notes
- Unity callbacks: Start(), Update(), OnTriggerEnter(), etc.
Example:
void AttackEnemy() {
Debug.Log("Enemy attacked!");
4. Control Statements
- if, else, switch
- loops: for, while, foreach
Example:
if (health <= 0) {
Die();
5. Unity Specific Classes
- Transform: position, rotation, scale
- Rigidbody: physics, force, gravity
- Collider: trigger events, collisions
- GameObject: creation, destruction, components
C# for Unity and Game Development - Complete Notes
Example:
transform.Translate(Vector3.forward * speed * Time.deltaTime);
6. Input Handling
- Input.GetKey(), GetAxis(), GetMouseButton()
Example:
float move = Input.GetAxis("Vertical");
transform.Translate(Vector3.forward * move);
7. Coroutines
Used for delay/timers without blocking the main thread.
Example:
IEnumerator WaitBeforeAttack() {
yield return new WaitForSeconds(2f);
AttackEnemy();
}
C# for Unity and Game Development - Complete Notes
8. Object-Oriented Concepts
- Class, Object, Inheritance, Encapsulation, Polymorphism
- Unity uses Components as modular objects
Example:
public class Enemy : Character {}
9. Prefabs and Instantiation
- Prefab: Reusable GameObject template
- Instantiate(prefab), Destroy(gameObject)
Example:
Instantiate(bulletPrefab, transform.position, Quaternion.identity);
10. UI in Unity
- UI Elements: Text, Button, Slider, Canvas
- EventSystem handles UI interactions
Example:
public Text scoreText;
C# for Unity and Game Development - Complete Notes
scoreText.text = "Score: " + score;
11. Scene Management
- UnityEngine.SceneManagement
- SceneManager.LoadScene("Level2");
Useful for loading new levels or menus.
12. ScriptableObjects
- Data containers that save states independent of scenes.
Create via: CreateAssetMenu
Example:
[CreateAssetMenu(menuName = "Item")]
public class Item : ScriptableObject {}
13. Best Practices
- Use descriptive variable names
- Avoid using Update() heavily
C# for Unity and Game Development - Complete Notes
- Use SerializeField over public for private encapsulation
- Modularize scripts
- Use Events & Delegates for decoupled systems
14. Final Tips for Game Dev with C#
- Plan your systems with diagrams
- Use version control (Git)
- Use asset store tools/plugins wisely
- Test regularly and optimize for performance
- Focus on player experience and fun