using UnityEngine; using Unity.Netcode; namespace Northbound { /// /// Validates NetworkManager configuration before starting server /// Checks for common issues that prevent server from starting /// public class NetworkManagerValidator : MonoBehaviour { [Header("Validation Settings")] [SerializeField] private bool validateOnStart = true; [SerializeField] private bool validateOnEnable = true; [SerializeField] private bool showDetailedLogs = true; [Header("Auto-Fix")] [SerializeField] private bool autoFixIfPossible = true; private NetworkManager _networkManager; private bool _hasValidated = false; private void Awake() { StartCoroutine(InitializeDelayed()); } private System.Collections.IEnumerator InitializeDelayed() { Debug.Log("[NetworkValidator] Waiting for NetworkManager to initialize..."); int attempts = 0; while (_networkManager == null && attempts < 50) { _networkManager = NetworkManager.Singleton; if (_networkManager == null) { yield return new WaitForSeconds(0.1f); attempts++; } } if (_networkManager == null) { Debug.LogError("[NetworkValidator] ❌ CRITICAL: NetworkManager not found after 50 attempts!"); Debug.LogError("[NetworkValidator] Ensure NetworkManager is in scene and has 'NetworkManager' component"); yield break; } Debug.Log("[NetworkValidator] NetworkManager found!"); if (validateOnAwake) { yield return new WaitForSeconds(0.5f); ValidateConfiguration(); } } private void Start() { if (validateOnStart && !_hasValidated) { ValidateConfiguration(); } } private void OnEnable() { if (validateOnEnable && !_hasValidated) { ValidateConfiguration(); } } [ContextMenu("Validate NetworkManager Configuration")] public void ValidateConfiguration() { Debug.Log("=== NETWORKMANAGER VALIDATION ==="); if (_networkManager == null) { Debug.LogError("[NetworkValidator] ❌ NetworkManager is NULL!"); Debug.LogError("[NetworkValidator] Cannot validate configuration"); return; } _hasValidated = true; bool allValid = true; allValid &= CheckPlayerPrefab(); allValid &= CheckNetworkConfig(); allValid &= CheckTransport(); allValid &= CheckRequiredComponents(); Debug.Log(""); if (allValid) { Debug.Log("[NetworkValidator] ✅ ALL CHECKS PASSED - NetworkManager is ready!"); Debug.Log("[NetworkValidator] Server should start successfully"); } else { Debug.LogError("[NetworkValidator] ❌ VALIDATION FAILED - Server may NOT start!"); Debug.LogError("[NetworkValidator] Please fix the issues above"); } Debug.Log("================================="); } private bool CheckPlayerPrefab() { Debug.Log("[NetworkValidator] 1. Checking Player Prefab..."); if (_networkManager.NetworkConfig == null) { Debug.LogError("[NetworkValidator] ❌ NetworkConfig is NULL!"); return false; } GameObject playerPrefab = _networkManager.NetworkConfig.PlayerPrefab; if (playerPrefab == null) { Debug.LogError("[NetworkValidator] ❌ Player Prefab is NOT assigned!"); Debug.LogError("[NetworkValidator] This is the #1 cause of servers not starting!"); Debug.LogError("[NetworkValidator] FIX:"); Debug.LogError("1. Select NetworkManager in scene"); Debug.LogError("2. Inspector → Network Config → Player Prefab"); Debug.LogError("3. Drag your Player Prefab from Project window"); Debug.LogError("4. Assign to Player Prefab field"); return false; } Debug.Log("[NetworkValidator] ✅ Player Prefab assigned: " + playerPrefab.name + ""); NetworkObject networkObject = playerPrefab.GetComponent(); if (networkObject == null) { Debug.LogWarning("[NetworkValidator] ⚠ Player Prefab has NO NetworkObject!"); Debug.LogWarning("[NetworkValidator] FIX: Add NetworkObject component to Player Prefab"); return false; } Debug.Log("[NetworkValidator] ✅ Player Prefab has NetworkObject"); return true; } private bool CheckNetworkConfig() { Debug.Log("[NetworkValidator] 2. Checking Network Config..."); if (_networkManager.NetworkConfig == null) { Debug.LogError("[NetworkValidator] ❌ NetworkConfig is NULL!"); return false; } bool configValid = true; bool connectionApproval = _networkManager.NetworkConfig.ConnectionApproval; if (connectionApproval) { Debug.Log("[NetworkValidator] ✅ Connection Approval: ENABLED"); } else { Debug.LogWarning("[NetworkValidator] ⚠ Connection Approval: DISABLED"); Debug.LogWarning("[NetworkValidator] All connections will be auto-approved"); } bool sceneManagement = _networkManager.NetworkConfig.EnableSceneManagement; if (sceneManagement) { Debug.Log("[NetworkValidator] ✅ Scene Management: ENABLED"); } else { Debug.LogWarning("[NetworkValidator] ⚠ Scene Management: DISABLED"); } float clientBuffer = _networkManager.NetworkConfig.ClientConnectionBufferTimeout; Debug.Log("[NetworkValidator] Client Connection Buffer: " + clientBuffer + "s"); return configValid; } private bool CheckTransport() { Debug.Log("[NetworkValidator] 3. Checking Transport..."); var transport = _networkManager.GetComponent(); if (transport == null) { Debug.LogError("[NetworkValidator] ❌ UnityTransport not found!"); Debug.LogError("[NetworkValidator] FIX: Add Unity Transport component to NetworkManager"); return false; } Debug.Log("[NetworkValidator] ✅ Unity Transport exists"); ushort port = transport.ConnectionData.Port; Debug.Log("[NetworkValidator] Transport Port: " + port); if (port <= 0) { Debug.LogWarning("[NetworkValidator] ⚠ Transport Port is not set (0)"); Debug.LogWarning("[NetworkValidator] FIX: Set port to 40445 in Unity Transport component"); return false; } string address = transport.ConnectionData.Address; Debug.Log("[NetworkValidator] Transport Address: " + address); if (address == "127.0.0.1") { Debug.LogError("[NetworkValidator] ❌ Transport Address is 127.0.0.1!"); Debug.LogError("[NetworkValidator] This blocks ALL remote connections!"); Debug.LogError("[NetworkValidator] FIX: Set to 0.0.0.0"); return false; } if (address == "0.0.0.0") { Debug.Log("[NetworkValidator] ✅ Transport Address: 0.0.0.0 (accepts all connections)"); } return true; } private bool CheckRequiredComponents() { Debug.Log("[NetworkValidator] 4. Checking Required Components..."); bool allPresent = true; NetworkConnectionHandler connectionHandler = FindObjectOfType(); if (connectionHandler != null) { Debug.Log("[NetworkValidator] ✅ NetworkConnectionHandler found"); } else { Debug.LogWarning("[NetworkValidator] ⚠ NetworkConnectionHandler NOT found"); Debug.LogWarning("[NetworkValidator] FIX: Add NetworkConnectionHandler to scene"); Debug.LogWarning("[NetworkValidator] Required for manual player spawning"); } PlayerSpawnPoint[] spawnPoints = FindObjectsByType(FindObjectsSortMode.None); if (spawnPoints.Length > 0) { Debug.Log("[NetworkValidator] ✅ Found " + spawnPoints.Length + " Player Spawn Point(s)"); } else { Debug.LogWarning("[NetworkValidator] ⚠ No Player Spawn Points found"); Debug.LogWarning("[NetworkValidator] FIX: Add PlayerSpawnPoint component to empty GameObject(s)"); } AutoHost autoHost = FindObjectOfType(); if (autoHost != null && autoHost.enabled) { Debug.LogWarning("[NetworkValidator] ⚠ AutoHost is enabled!"); Debug.LogWarning("[NetworkValidator] This might interfere with manual host startup"); Debug.LogWarning("[NetworkValidator] Consider disabling AutoHost"); } return allPresent; } [ContextMenu("Generate Fix Report")] public void GenerateFixReport() { Debug.Log("=== NETWORKMANAGER FIX REPORT ==="); Debug.Log(""); Debug.Log("PROBLEM: PortListenerTest works, but Unity Netcode doesn't"); Debug.Log(""); Debug.Log("MOST LIKELY CAUSE:"); Debug.LogError("1. ❌ Player Prefab is NOT assigned (90% probability)"); Debug.LogWarning("2. ⚠ NetworkManager has initialization order issue"); Debug.LogWarning("3. ⚠ Transport not configured correctly"); Debug.LogWarning("4. ⚠ AutoHost interfering"); Debug.Log(""); Debug.Log("QUICK FIX:"); Debug.Log("1. Select NetworkManager in scene"); Debug.Log("2. Inspector → Network Config → Player Prefab"); Debug.Log("3. Drag your Player Prefab from Project window"); Debug.Log("4. Drop into Player Prefab field"); Debug.Log("5. Save scene"); Debug.Log("6. Start Host"); Debug.Log("7. Test on yougetsignal IMMEDIATELY"); Debug.Log(""); Debug.Log("COMPLETE FIX:"); Debug.Log("1. Right-click this component → 'Validate NetworkManager Configuration'"); Debug.Log("2. Fix any errors shown"); Debug.Log("3. Add ForceTransportBinding"); Debug.Log("4. Right-click → 'Force Then Start Host'"); Debug.Log("5. Add ContinuousPortMonitor"); Debug.Log("6. Right-click → 'Start Port Monitoring'"); Debug.Log("7. Watch Console for port status"); Debug.Log("================================="); } [ContextMenu("Check If Server Can Start")] public void CheckIfServerCanStart() { Debug.Log("=== CAN SERVER START? ==="); bool canStart = true; if (_networkManager == null) { Debug.LogError("❌ NO - NetworkManager is NULL"); canStart = false; } else { if (_networkManager.NetworkConfig?.PlayerPrefab == null) { Debug.LogError("❌ NO - Player Prefab NOT assigned"); canStart = false; } if (_networkManager.GetComponent() == null) { Debug.LogError("❌ NO - UnityTransport NOT found"); canStart = false; } } if (canStart) { Debug.Log("✅ YES - Server should be able to start"); Debug.Log("If port still closed after starting:"); Debug.Log("1. Check Console for errors"); Debug.Log("2. Verify Port Forwarding is working"); Debug.Log("3. Verify Firewall is allowing port"); } Debug.Log("========================"); } private bool validateOnAwake = true; } }