Gemini modifications
Gemini Modifications & Improvements
This note contains suggested improvements and expanded content for your C# and Unity language notes. The goal is to move from basic syntax to more practical game development application.
1. Structural Improvements (Applied to Current Notes)
Data Types: The "Memory" Context
You have a great start. Adding Value vs. Reference types is a huge level-up for C#.
- Value Types: (
int,bool,float,Vector3,struct) Store actual data. When passed around, you make a copy. - Reference Types: (
string,class,GameObject,Transform) Store a reference to where the data is in memory.
Access Modifiers: Beyond "Public"
You noted public. In Unity, we also use:
- private (Default): Only this script can see it. Good for internal logic.
- [SerializeField]: The "Unity Secret Weapon." It keeps a variable
private(safe from other scripts) but still shows it in the Inspector.
[SerializeField] private float speed = 5.0f; // Safe and Editable!
2. Expanded Unity Concepts
Collections: Arrays vs. Lists
In games, you rarely have just one enemy. You have a collection of them.
- Array (
type[]): Fixed size. Fast. Use when you know exactly how many things you have (e.g.,WeaponSlot[] weapons = new WeaponSlot[3];). - List (
List<T>): Dynamic size. Can add/remove items. Requiresusing System.Collections.Generic;.
Input System (Modern vs. Legacy)
You mentioned ReadValue<>. This suggests you are using the New Input System. Here is a quick reference:
// To move based on input
Vector2 moveInput = moveAction.ReadValue<Vector2>();
transform.Translate(new Vector3(moveInput.x, 0, moveInput.y) * speed * Time.deltaTime);
The Importance of Time.deltaTime
When moving objects in Update(), always multiply by Time.deltaTime.
- Why?
Updateruns every frame. On a fast PC (144fps), things move faster than on a slow PC (30fps) unless you normalize it withTime.deltaTime(seconds per frame).
3. Advanced Logical Flow
Ternary Operator (The One-Line "If")
Great for simple assignments.
string status = (health > 0) ? "Alive" : "Dead";
Null Checking
The #1 error in Unity is the NullReferenceException.
- The Safe Way:
if (target != null) { ... } - The Pro Way (Null Conditional):
target?.TakeDamage(10);(Only calls the method if target isn't null).
4. Suggested New Categories to Add
Coroutines (IEnumerator)
Used for things that happen over time (fading out, waiting for a cooldown).
IEnumerator Cooldown() {
canShoot = false;
yield return new WaitForSeconds(2f);
canShoot = true;
}
Events & Actions
Allows scripts to talk to each other without being "stuck" together (Decoupling).
- Example: Player dies -> Player script sends an "OnPlayerDeath" event -> UI shows Game Over screen, Sound plays sad music.
Namespaces
Always know what using does:
using UnityEngine;-> Gives access toGameObject,MonoBehaviour,Vector3.using UnityEngine.UI;-> Gives access toText,Button,Image.
5. Clean Code Tips
- PascalCase for Methods and Classes:
void TakeDamage(). - camelCase for variables:
float moveSpeed. - Use Comments for "Why", not "What".
- Bad:
x++; // increment x - Good:
x++; // move to next waypoint in the patrol path
- Bad: