C# BASICS FOR UNITY ENGINE – EASY NOTES
1. What is Unity and Why C#?
Unity is a popular game engine used to create 2D, 3D, AR, and VR games. C# is the main
programming language used in Unity.
It allows you to control game logic, physics, animations, user input, and much more.
2. Structure of a Unity Script
Each C# script in Unity usually inherits from MonoBehaviour.
Example:
using UnityEngine;
public class Player : MonoBehaviour
{
void Start()
{
// Called once when the game starts
[Link]("Game Started!");
}
void Update()
{
// Called every frame
[Link]("Frame Updated");
}
}
Explanation:
- MonoBehaviour: Base class for all Unity scripts.
- Start(): Runs once when the game begins.
- Update(): Runs every frame (used for movement, input, etc.).
3. Variables and Data Types in Unity
Example:
public int health = 100;
private float speed = 5.5f;
[SerializeField] private string playerName = "Hero";
Explanation:
- public: Visible and editable in the Unity Inspector.
- private: Hidden from Inspector (used internally).
- [SerializeField]: Keeps variable private but still visible in Inspector.
4. GameObjects and Components
In Unity, everything in a scene is a GameObject. Components add behavior or data to
them.
Example:
public class Example : MonoBehaviour
{
void Start()
{
GameObject cube = [Link]("Cube");
[Link]<Renderer>().[Link] = [Link];
}
}
Explanation:
- [Link](): Finds an object in the scene.
- GetComponent(): Accesses a component attached to that object (like Renderer,
Rigidbody, etc.).
5. Transform and Movement
Example:
public class Move : MonoBehaviour
{
void Update()
{
[Link]([Link] * [Link]);
}
}
Explanation:
- [Link](): Moves the object.
- [Link]: Direction (Z-axis).
- [Link]: Keeps movement smooth across different frame rates.
6. Input Handling
Example:
public class PlayerMove : MonoBehaviour
{
void Update()
{
float move = [Link]("Horizontal");
[Link]([Link] * move * [Link]);
}
}
Explanation:
- [Link]("Horizontal"): Reads keyboard input (A/D or Left/Right arrows).
7. Collisions and Triggers
Example:
void OnCollisionEnter(Collision other)
{
[Link]("Hit " + [Link]);
}
void OnTriggerEnter(Collider other)
{
[Link]("Entered trigger of " + [Link]);
}
Explanation:
- OnCollisionEnter(): Called when objects with colliders collide.
- OnTriggerEnter(): Called when an object enters a trigger zone.
8. Coroutines
Used for delays or timed actions.
Example:
IEnumerator WaitAndShoot()
{
yield return new WaitForSeconds(2f);
[Link]("Shoot after 2 seconds!");
}
void Start()
{
StartCoroutine(WaitAndShoot());
}
Explanation:
- IEnumerator: Allows pausing execution.
- WaitForSeconds(): Delays execution by a specific time.
9. Instantiation and Prefabs
Example:
public GameObject bulletPrefab;
void Update()
{
if ([Link]([Link]))
{
Instantiate(bulletPrefab, [Link], [Link]);
}
}
Explanation:
- Instantiate(): Creates a copy of an object (like bullets or enemies).
- Prefabs: Pre-made templates of GameObjects you can spawn anytime.
10. Summary and Tips
- Every Unity script inherits from MonoBehaviour.
- Start() and Update() are most used methods.
- Use GetComponent<> to access other components.
- Coroutines are powerful for timing actions.
- Keep code organized and readable.
Happy Coding! ■