# Building Ghost Preview Troubleshooting

## FIXED: Preview Activated But Not Visible

If console shows "Preview activated" but you can't see it, this was a **material/transparency issue**.

**Solution implemented:**
- Better material creation supporting both URP and Standard RP
- Proper transparency setup for ghost materials
- Multiple material slots handled correctly
- Auto-creates debug cube if prefab has no renderer

**Test the fix:**
1. Press Play
2. Press B to enter build mode
3. Move mouse over ground
4. You should now see a **green transparent ghost** of your building
5. Invalid positions show **red transparent ghost**

If still not visible after update, continue with checklist below.

---

## Quick Checklist

Run through these checks in order:

### 1. Check Console Logs
Press B to enter build mode and look for these messages:
- ✅ "Entered Build Mode"
- ✅ "[BuildingPlacement] Created preview: BuildingPreview"
- ✅ "[BuildingPlacement] Found X renderers"
- ✅ "[BuildingPlacement] Applied valid material to preview"
- ✅ "[BuildingPlacement] Preview activated at (x, y, z)"

**If you see errors:**
- ❌ "BuildingManager.Instance is null" → Go to Step 2
- ❌ "No buildings available" → Go to Step 3
- ❌ "Prefab is null" → Go to Step 4
- ❌ "Preview deactivated - no ground hit" → Go to Step 5

### 2. Check BuildingManager Setup
1. In Hierarchy, find "BuildingManager" GameObject
2. Verify it has:
   - `BuildingManager` component
   - `NetworkObject` component
3. In BuildingManager component:
   - **Grid Size**: Should be > 0 (e.g., 1)
   - **Ground Layer**: Should be set (e.g., "Default" or "Ground")
   - **Available Buildings**: Should have at least 1 BuildingData

**Fix:** If BuildingManager doesn't exist, create it:
- Create Empty GameObject → Name: "BuildingManager"
- Add Component → BuildingManager
- Add Component → NetworkObject
- Configure settings

### 3. Check BuildingData Asset
1. Project window → Find your BuildingData asset
2. Select it and check Inspector:
   - **Building Name**: Should have a name
   - **Prefab**: Should be assigned (not "None")
   - **Width/Length/Height**: Should be > 0
3. Verify this BuildingData is in BuildingManager's "Available Buildings" list

**Fix:** If BuildingData missing:
- Right-click in Project → Create → Northbound → Building Data
- Assign a prefab
- Add to BuildingManager's Available Buildings list

### 4. Check Building Prefab
1. Find your building prefab in Project
2. Open it and verify:
   - Has a `MeshRenderer` or `SkinnedMeshRenderer` component
   - Renderer has a `Material` assigned
   - Has a `Collider` (for placement validation)
   - Optionally has `NetworkObject` (will be removed from preview)

**Fix:** If prefab is missing components:
- Add a 3D object (Cube, for testing)
- Save as prefab
- Assign to BuildingData

### 5. Check Ground Layer
The preview won't show if the raycast can't hit the ground.

1. In Scene view, select your terrain/ground
2. Check its **Layer** (top of Inspector)
3. In BuildingManager:
   - **Ground Layer** mask should include this layer
4. In BuildingPlacement (on Player):
   - **Ground Layer** mask should match BuildingManager

**Fix:**
- Set terrain Layer to "Default" or create "Ground" layer
- In BuildingManager/BuildingPlacement → Ground Layer → Check "Default" (or your ground layer)

### 6. Check Camera
1. Verify `Camera.main` exists in scene
2. Camera should be able to see the ground

**Fix:**
- Make sure Main Camera has tag "MainCamera"
- Camera should be positioned to see the scene

### 7. Check Player Setup
1. Find your Player prefab
2. Verify it has:
   - `BuildingPlacement` component
   - **Ground Layer** is set
   - **Max Placement Distance** > 0 (e.g., 100)

### 8. Visual Check in Scene View
1. Press B to enter build mode
2. Open **Hierarchy** window
3. Look for object named "BuildingPreview"
4. If it exists:
   - Click on it
   - Check if it's active (checkbox next to name)
   - Look at its position in Inspector
   - Switch to **Scene** view and look for it
   - Check if materials are assigned in Renderer component

### 9. Test with Simple Cube
Create a minimal test setup:
1. Create a Cube in scene → Save as Prefab → Delete from scene
2. Create BuildingData → Assign Cube prefab
3. Add to BuildingManager → Available Buildings
4. Create a Plane → Position at (0, 0, 0)
5. Press Play → Press B → Move mouse over Plane

You should see the transparent cube preview.

## Common Issues

### Preview exists but is invisible
**Cause:** Materials not rendering or transparent issues
**Fix (Now Automated):**
- The system now auto-detects your render pipeline (URP or Standard)
- Creates proper transparent materials automatically
- If prefab has no renderer, adds a debug cube

**Manual Test:**
1. Add `GhostMaterialTest` component to any object with a renderer
2. Press Play
3. Object should become transparent green
4. If it works, the ghost system will work too

### Preview doesn't follow mouse
**Cause:** Ground layer not set correctly or no ground detected
**Fix:**
- Check console for "no ground hit" message
- Verify Ground Layer includes your terrain
- Increase Max Placement Distance
- Make sure you're pointing at ground, not sky

### Preview spawns at (0,0,0) and doesn't move
**Cause:** Raycast not hitting ground
**Fix:**
- Same as above - Ground Layer issue

### Materials are black/pink
**Cause:** Shader/material issues
**Fix:**
- Make sure you're using the correct render pipeline shaders
- Standard RP: Use "Standard" shader
- URP: Use "Universal Render Pipeline/Lit" shader

## Debug Commands

Add these temporary debug lines to check:

In `CreatePreview()` after instantiation:
```csharp
Debug.Log($"Preview position: {previewObject.transform.position}");
Debug.Log($"Preview active: {previewObject.activeSelf}");
Debug.Log($"Renderers: {previewRenderers.Length}");
```

In `UpdatePreviewPosition()`:
```csharp
Debug.DrawRay(ray.origin, ray.direction * maxPlacementDistance, Color.red);
```

This shows the raycast in Scene view (red line from camera to mouse).

## Still Not Working?

Check these final items:
1. Input Actions are set up (ToggleBuildMode, Rotate, Build)
2. BuildingManager is spawned on network (in scene, not prefab)
3. You're the owner of the player (IsOwner = true)
4. No errors in Console
5. Build mode is actually active (check console for "Entered Build Mode")

If all else fails, check the Console for any errors and share them!
