using UnityEngine; /// /// Debug script to diagnose settlement interaction issues /// Add this to a GameObject in MapScene2 to test the settlement interaction system /// public class SettlementInteractionDebugger : MonoBehaviour { [Header("Debug Settings")] public bool enableDebugOverlay = true; public bool logEveryFrame = false; private SettlementInteractionManager interactionManager; private SettlementInteractionUI interactionUI; private Transform teamMarker; void Start() { Debug.Log("=== Settlement Interaction Debugger Started ==="); // Find components interactionManager = FindFirstObjectByType(); interactionUI = FindFirstObjectByType(); teamMarker = GameObject.Find("TeamMarker")?.transform; // Initial diagnostics DiagnoseComponents(); } void Update() { if (logEveryFrame) { LogCurrentState(); } // Test manual input if (Input.GetKeyDown(KeyCode.F1)) { TestSettlementDetection(); } if (Input.GetKeyDown(KeyCode.F2)) { TestManualEntry(); } } private void DiagnoseComponents() { Debug.Log("=== Component Diagnosis ==="); // Check SettlementInteractionManager if (interactionManager != null) { Debug.Log("✅ SettlementInteractionManager found"); Debug.Log($" - GameObject: {interactionManager.gameObject.name}"); Debug.Log($" - Enabled: {interactionManager.enabled}"); Debug.Log($" - Active: {interactionManager.gameObject.activeSelf}"); Debug.Log($" - TeamMarker assigned: {interactionManager.teamMarker != null}"); Debug.Log($" - Interaction Distance: {interactionManager.interactionDistance}"); Debug.Log($" - Enter Key: {interactionManager.enterSettlementKey}"); Debug.Log($" - Debug Logs Enabled: {interactionManager.enableDebugLogs}"); // Check if MapData is accessible var mapMaker = FindFirstObjectByType(); if (mapMaker != null) { var mapData = mapMaker.GetMapData(); Debug.Log($" - MapData available: {mapData != null}"); if (mapData != null) { var settlements = mapData.GetAllSettlements(); Debug.Log($" - Settlements count: {settlements?.Count ?? 0}"); } } else { Debug.LogWarning(" - MapMaker2 not found!"); } } else { Debug.LogError("❌ SettlementInteractionManager NOT FOUND!"); Debug.LogError(" Make sure to add SettlementInteractionManager component to a GameObject in MapScene2"); } // Check SettlementInteractionUI if (interactionUI != null) { Debug.Log("✅ SettlementInteractionUI found"); Debug.Log($" - GameObject: {interactionUI.gameObject.name}"); } else { Debug.LogWarning("⚠️ SettlementInteractionUI not found"); } // Check TeamMarker if (teamMarker != null) { Debug.Log("✅ TeamMarker found"); Debug.Log($" - Position: {teamMarker.position}"); } else { Debug.LogError("❌ TeamMarker GameObject not found!"); } Debug.Log("=== End Diagnosis ==="); Debug.Log("Press F1 to test settlement detection"); Debug.Log("Press F2 to test manual settlement entry"); } private void LogCurrentState() { if (interactionManager == null || teamMarker == null) return; var currentSettlement = interactionManager.GetCurrentNearbySettlement(); Debug.Log($"[Frame] TeamMarker: {teamMarker.position}, Current Settlement: {(currentSettlement?.name ?? "None")}"); } [ContextMenu("Test Settlement Detection")] public void TestSettlementDetection() { Debug.Log("=== Testing Settlement Detection ==="); if (interactionManager == null) { Debug.LogError("No SettlementInteractionManager found!"); return; } if (teamMarker == null) { Debug.LogError("No TeamMarker found!"); return; } var currentSettlement = interactionManager.GetCurrentNearbySettlement(); Debug.Log($"Current nearby settlement: {(currentSettlement?.name ?? "None")}"); // Check if UI is showing if (interactionUI != null) { Debug.Log("SettlementInteractionUI is present and should be handling events"); } // Manual coordinate check Vector2 teamWorldPos = new Vector2(teamMarker.position.x, teamMarker.position.z); Debug.Log($"TeamMarker world position: {teamWorldPos}"); // Try to find MapData and check settlements manually var mapMaker = FindFirstObjectByType(); if (mapMaker != null) { var mapData = mapMaker.GetMapData(); if (mapData != null) { var settlements = mapData.GetAllSettlements(); Debug.Log($"Total settlements in map: {settlements?.Count ?? 0}"); if (settlements != null && settlements.Count > 0) { foreach (var settlement in settlements) { float distance = Vector2.Distance(teamWorldPos, new Vector2(settlement.position.x, settlement.position.y)); Debug.Log($"Settlement '{settlement.name}' at {settlement.position} - Distance: {distance:F2}"); } } } } } [ContextMenu("Test Manual Entry")] public void TestManualEntry() { Debug.Log("=== Testing Manual Settlement Entry ==="); if (interactionManager != null) { interactionManager.TestEnterCurrentSettlement(); } else { Debug.LogError("No SettlementInteractionManager found!"); } } [ContextMenu("Force Enable Debug Logging")] public void ForceEnableDebugLogging() { if (interactionManager != null) { interactionManager.enableDebugLogs = true; Debug.Log("Debug logging enabled on SettlementInteractionManager"); } } [ContextMenu("Test Input Detection")] public void TestInputDetection() { Debug.Log("=== Testing Input Detection ==="); Debug.Log($"Input.GetKeyDown(KeyCode.E): {Input.GetKeyDown(KeyCode.E)}"); Debug.Log($"Input.GetKey(KeyCode.E): {Input.GetKey(KeyCode.E)}"); Debug.Log($"Input.inputString: '{Input.inputString}'"); } void OnGUI() { if (!enableDebugOverlay) return; GUILayout.BeginArea(new Rect(10, 10, 400, 300)); GUILayout.BeginVertical("box"); GUILayout.Label("Settlement Interaction Debug", new GUIStyle(GUI.skin.label) { fontSize = 16, fontStyle = FontStyle.Bold }); if (interactionManager != null) { var currentSettlement = interactionManager.GetCurrentNearbySettlement(); GUILayout.Label($"Current Settlement: {(currentSettlement?.name ?? "None")}"); if (teamMarker != null) { GUILayout.Label($"TeamMarker Position: {teamMarker.position}"); } GUILayout.Space(10); if (GUILayout.Button("Test Settlement Detection (F1)")) { TestSettlementDetection(); } if (GUILayout.Button("Test Manual Entry (F2)")) { TestManualEntry(); } if (GUILayout.Button("Force Debug Logging")) { ForceEnableDebugLogging(); } } else { GUILayout.Label("❌ SettlementInteractionManager NOT FOUND!", new GUIStyle(GUI.skin.label) { normal = { textColor = Color.red } }); GUILayout.Label("Add SettlementInteractionManager component to a GameObject in this scene"); } GUILayout.EndVertical(); GUILayout.EndArea(); } }