0% found this document useful (0 votes)
7 views4 pages

CSharp Basics For Unity Engine

This document provides a concise overview of C# basics for Unity Engine, covering key concepts such as Unity's purpose, script structure, variables, GameObjects, movement, input handling, collisions, coroutines, and instantiation. It emphasizes the importance of MonoBehaviour, Start() and Update() methods, and offers tips for organizing code. The document serves as a quick reference for beginners in Unity game development using C#.

Uploaded by

tawac24212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

CSharp Basics For Unity Engine

This document provides a concise overview of C# basics for Unity Engine, covering key concepts such as Unity's purpose, script structure, variables, GameObjects, movement, input handling, collisions, coroutines, and instantiation. It emphasizes the importance of MonoBehaviour, Start() and Update() methods, and offers tips for organizing code. The document serves as a quick reference for beginners in Unity game development using C#.

Uploaded by

tawac24212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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! ■

You might also like