How to Use Unity: 2D, 3D, C# Scripting & Mobile Deployment Guide
Key Takeaways
- Unity uses a component-based workflow: attach scripts and assets to GameObjects, not monolithic code.
- For 2D, set up a Sprite Renderer and use `Vector2` movement; for 3D, enable colliders and use `Vector3`.
- C# scripts in Unity run via `Start()` (once) and `Update()` (every frame)—keep `Update()` lightweight.
- Mobile deployment requires adjusting build settings, testing on device, and optimizing with the Profiler.
---
Getting Started: The Unity Interface
When you first open Unity, you see five panels: Scene, Game, Hierarchy, Project, and Inspector. I remember my first time—I dragged a cube into the Scene and couldn't figure out why it had no physics. The answer: Unity treats everything as a GameObject with Components. A cube is just a mesh renderer until you add a Rigidbody and Collider.
Pro tip: Always name your GameObjects clearly. I once spent an hour debugging "Cube (1)" in a scene with 50 cubes.
2D Setup vs 3D Setup
Unity defaults to 3D, but switching to 2D is one click: Edit > Project Settings > Editor > Default Behavior Mode > 2D. This changes the camera to orthographic (no perspective) and adds Sprite Renderer by default.
| Feature | 2D Mode | 3D Mode |
| --------- | --------- | --------- |
| Camera | Orthographic | Perspective |
| Primary Visual | Sprites (Sprite Renderer) | Meshes (Mesh Renderer) |
| Movement Vector | `Vector2` | `Vector3` |
| Collider Example | BoxCollider2D | BoxCollider |
Example: In my first 2D game, I used `transform.Translate(Vector2.right * speed * Time.deltaTime)` to move a player. In 3D, I'd swap `Vector2.right` for `Vector3.forward`.
C# Scripting: The Basics
Scripts are components you attach to GameObjects. Here's a simple player movement script for 2D:
```csharp
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed = 5f; // Adjustable in Inspector
void Update()
{
float moveX = Input.GetAxis("Horizontal");
Vector2 movement = new Vector2(moveX, 0);
transform.Translate(movement * speed * Time.deltaTime);
}
}
```
Key points:
- `public float speed` exposes the variable in the Inspector—you can tweak it without editing code.
- `Time.deltaTime` makes movement frame-rate independent. Without it, a 120 FPS PC would move twice as fast as a 60 FPS phone.
For 3D, replace `Vector2` with `Vector3` and add `Input.GetAxis("Vertical")` for forward/backward.
Building a Simple Scene
Let's make a 2D obstacle course:
1. Create a Sprite (right-click in Hierarchy > 2D Object > Sprite). Assign a texture from Assets.
2. Add a BoxCollider2D (in Inspector > Add Component).
3. Create a script called `ObstacleRotator`:
```csharp
void Update()
{
transform.Rotate(Vector3.forward * 90 * Time.deltaTime);
}
```
This rotates the obstacle 90 degrees per second.
Real numbers: In a project I worked on, we had 200 obstacles with rotation scripts. The frame rate dropped from 60 to 45 FPS because each `Update()` call had overhead. We fixed it by batching rotations with a single script controlling multiple objects.
Mobile Deployment: Android & iOS
Publishing to mobile requires a few steps:
For Android:
1. Install Android Build Support (via Unity Hub > Add Modules).
2. Switch Platform: File > Build Settings > Android > Switch Platform.
3. Set Player Settings: Under Player > Resolution, set default orientation to Landscape or Portrait. Enable Internet Access if using ads.
4. Build: Click Build and generate an `.apk` or `.aab`. I usually test with `.apk` first, then switch to `.aab` for Google Play.
For iOS:
1. You need a Mac with Xcode installed.
2. Player Settings > iOS: Set Bundle Identifier (e.g., `com.yourcompany.yourgame`).
3. Build generates an Xcode project. Open it, connect your iPhone, and hit Run.
Performance tip: Use the Profiler window (Window > Analysis > Profiler) before building. On a test project, I saw draw calls jump from 50 to 300 on mobile because of unscaled textures. I reduced texture sizes from 2048x2048 to 512x512, and the game ran at 60 FPS on a 3-year-old phone.
Common Beginner Mistakes (and Fixes)
- Forgetting `Time.deltaTime`: Your game runs at different speeds on different devices. Always multiply movement by `Time.deltaTime`.
- Not adjusting Collider size: A BoxCollider2D often defaults to 1x1 unit. If your sprite is 2x2, resize the collider in the Inspector or it won't match.
- Overusing `Find` methods: `GameObject.Find("Player")` is slow. Use public references (drag objects in the Inspector) or singletons.
FAQ
Q: Why does my script show an error "The type or namespace name 'Vector2' could not be found"?
A: You need to add `using UnityEngine;` at the top of your script. Vector2 lives in that namespace.
Q: How do I make a 3D character move with WASD?
A: Use `Input.GetAxis("Horizontal")` for left/right and `Input.GetAxis("Vertical")` for forward/backward. Apply to `transform.Translate(new Vector3(horizontal, 0, vertical) * speed * Time.deltaTime);`.
Q: My game runs fine in the editor but lags on mobile. What's wrong?
A: Check draw calls in the Profiler. Common culprits: too many realtime lights, large textures, or unoptimized scripts. Try reducing texture sizes, baking lighting, and using object pooling instead of Instantiate/Destroy.