using UnityEngine; using Unity.Netcode; using Unity.Netcode.Transports.UTP; namespace Northbound { public class ConnectionDiagnostics : MonoBehaviour { [Header("Auto-Diagnosis")] [SerializeField] private bool runOnStart = true; [SerializeField] private bool autoFixIssues = true; private NetworkManager _networkManager; private UnityTransport _transport; private void Start() { if (runOnStart) { RunDiagnostics(); } } public void RunDiagnostics() { Debug.Log("=== RUNNING CONNECTION DIAGNOSTICS ==="); InitializeComponents(); CheckNetworkManager(); CheckTransport(); CheckPlayerPrefab(); CheckConnectionApproval(); if (autoFixIssues) { FixCommonIssues(); } GenerateReport(); Debug.Log("=== DIAGNOSTICS COMPLETE ==="); } private void InitializeComponents() { _networkManager = NetworkManager.Singleton; if (_networkManager == null) { Debug.LogError("[Diagnostics] NetworkManager not found!"); return; } _transport = _networkManager.GetComponent(); if (_transport == null) { Debug.LogError("[Diagnostics] UnityTransport not found!"); return; } } private void CheckNetworkManager() { Debug.Log("[1/5] Checking NetworkManager..."); if (_networkManager == null) { Debug.LogError("✗ NetworkManager is NULL"); return; } Debug.Log("✓ NetworkManager exists"); Debug.Log(" IsServer: " + _networkManager.IsServer); Debug.Log(" IsClient: " + _networkManager.IsClient); Debug.Log(" IsHost: " + _networkManager.IsHost); Debug.Log(" IsConnectedClient: " + _networkManager.IsConnectedClient); } private void CheckTransport() { Debug.Log("[2/5] Checking UnityTransport..."); if (_transport == null) { Debug.LogError("✗ UnityTransport is NULL"); return; } Debug.Log("✓ UnityTransport exists"); Debug.Log(" Connection Address: " + _transport.ConnectionData.Address); Debug.Log(" Connection Port: " + _transport.ConnectionData.Port); Debug.Log(" Server Listen Address: " + _transport.ConnectionData.ServerListenAddress); int timeout = _transport.ConnectTimeoutMS; if (timeout < 10000) { Debug.LogWarning("⚠ Connect timeout too low: " + timeout + "ms (recommended: 15000ms)"); } else { Debug.Log("✓ Connect timeout is adequate: " + timeout + "ms"); } ushort port = _transport.ConnectionData.Port; if (port <= 0 || port > 65535) { Debug.LogError("✗ Invalid port: " + port); } else { Debug.Log("✓ Port is valid: " + port); } } private void CheckPlayerPrefab() { Debug.Log("[3/5] Checking Player Prefab..."); if (_networkManager == null || _networkManager.NetworkConfig == null) { Debug.LogError("✗ NetworkConfig not accessible"); return; } GameObject playerPrefab = _networkManager.NetworkConfig.PlayerPrefab; if (playerPrefab == null) { Debug.LogError("✗ Player Prefab is NOT assigned!"); Debug.LogError(" → Players will not spawn"); Debug.LogError(" → Fix: Assign Player Prefab in NetworkManager"); return; } Debug.Log("✓ Player Prefab assigned: " + playerPrefab.name); NetworkObject networkObject = playerPrefab.GetComponent(); if (networkObject == null) { Debug.LogError("✗ Player Prefab has no NetworkObject component!"); } else { Debug.Log("✓ Player Prefab has NetworkObject"); } } private void CheckConnectionApproval() { Debug.Log("[4/5] Checking Connection Approval..."); if (_networkManager == null || _networkManager.NetworkConfig == null) { Debug.LogError("✗ NetworkConfig not accessible"); return; } bool approval = _networkManager.NetworkConfig.ConnectionApproval; Debug.Log("Connection Approval: " + approval); if (approval) { Debug.Log("✓ Connection approval is enabled"); Debug.Log(" → Ensure ConnectionApprovalCallback is set"); } else { Debug.LogWarning("⚠ Connection approval is disabled"); Debug.Log(" → Players may auto-connect without approval"); } NetworkConnectionHandler handler = FindObjectOfType(); if (handler != null) { Debug.Log("✓ NetworkConnectionHandler found"); } else { Debug.LogWarning("⚠ NetworkConnectionHandler not found"); Debug.Log(" → Manual player spawning may not work"); } } private void FixCommonIssues() { Debug.Log("[5/5] Fixing Common Issues..."); if (_transport == null) return; bool fixedSomething = false; if (_transport.ConnectTimeoutMS < 10000) { _transport.ConnectTimeoutMS = 15000; Debug.Log("✓ Fixed: Increased timeout to 15000ms"); fixedSomething = true; } if (_transport.MaxConnectAttempts < 5) { _transport.MaxConnectAttempts = 10; Debug.Log("✓ Fixed: Increased max connect attempts to 10"); fixedSomething = true; } ushort port = _transport.ConnectionData.Port; if (port <= 0) { _transport.SetConnectionData("0.0.0.0", 7777); Debug.Log("✓ Fixed: Set default port to 7777"); fixedSomething = true; } if (!fixedSomething) { Debug.Log("✓ No issues found to fix"); } } private void GenerateReport() { Debug.Log("=== DIAGNOSTICS REPORT ==="); string status = GetOverallStatus(); Color color = GetStatusColor(status); Debug.Log("" + status + ""); if (status != "System Healthy") { Debug.Log("Review warnings and errors above"); } Debug.Log("========================="); } private string GetOverallStatus() { if (_networkManager == null || _transport == null) return "Critical Errors Found"; if (_networkManager.NetworkConfig?.PlayerPrefab == null) return "Configuration Error"; if (_transport.ConnectTimeoutMS < 10000) return "Performance Issue"; return "System Healthy"; } private Color GetStatusColor(string status) { switch (status) { case "System Healthy": return Color.green; case "Performance Issue": return Color.yellow; case "Configuration Error": return new Color(1f, 0.5f, 0f); case "Critical Errors Found": return Color.red; default: return Color.gray; } } private string ColorToHex(Color color) { return "#" + ColorUtility.ToHtmlStringRGBA(color); } [ContextMenu("Run Diagnostics")] public void ManualRunDiagnostics() { RunDiagnostics(); } [ContextMenu("Check Connection Status")] public void CheckConnectionStatus() { if (_networkManager == null) { Debug.LogError("NetworkManager not found"); return; } Debug.Log("=== CONNECTION STATUS ==="); Debug.Log("IsServer: " + _networkManager.IsServer); Debug.Log("IsClient: " + _networkManager.IsClient); Debug.Log("IsHost: " + _networkManager.IsHost); Debug.Log("IsConnectedClient: " + _networkManager.IsConnectedClient); Debug.Log("LocalClientId: " + _networkManager.LocalClientId); Debug.Log("ConnectedClients: " + _networkManager.ConnectedClients.Count); if (_transport != null) { Debug.Log("Listening on: " + _transport.ConnectionData.Address + ":" + _transport.ConnectionData.Port); } if (_networkManager.IsHost || _networkManager.IsServer) { Debug.Log("Connected Players:"); foreach (var client in _networkManager.ConnectedClients) { string playerInfo = client.Value.PlayerObject != null ? client.Value.PlayerObject.name : "No Player Object"; Debug.Log(" Client " + client.Key + ": " + playerInfo); } } Debug.Log("======================="); } } }