Hey guys, need help with an issue with my game manager script. Context: I am making a boomerang fu knockoff developed for mobile multiplayer. My character has all the basic functions it needs ,and now I am making it so there is a game manager run on the first player to join’s side. This is the script:
using System;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : NetworkBehaviour
{
public static GameManager Instance;
public List<LilBoiMovement> players = new List<LilBoiMovement>();
private Dictionary<ulong, int> playerScores = new Dictionary<ulong, int>();
private NetworkVariable<bool> roundActive = new NetworkVariable<bool>(false);
private NetworkVariable<ulong> controllingPlayer = new NetworkVariable<ulong>(0);
private int maxPoints = 10;
public Transform[] spawnPoints;
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
}
public override void OnNetworkSpawn()
{
if (!IsOwner) return;
NetworkManager.Singleton.OnClientConnectedCallback += OnPlayerJoined;
if (IsLocalPlayer && NetworkManager.Singleton.ConnectedClients.Count == 1)
{
controllingPlayer.Value = NetworkManager.Singleton.LocalClientId;
}
Debug.Log("GameManager spawned. Controlled by Player " + controllingPlayer.Value);
}
private void OnPlayerJoined(ulong clientId)
{
if (!IsOwner) return;
Debug.Log("Player joined: " + clientId);
if (players.Count < 4)
{
SpawnPlayer(clientId);
}
if (players.Count == 4 && controllingPlayer.Value == NetworkManager.Singleton.LocalClientId)
{
Debug.Log("Four players joined. Starting round...");
StartNewRound();
}
}
private void SpawnPlayer(ulong clientId)
{
if (players.Count >= spawnPoints.Length)
{
Debug.LogWarning("No more spawn points available!");
return;
}
Debug.Log($"Spawning player {clientId} at spawn point {players.Count}");
GameObject playerObj = Instantiate(NetworkManager.Singleton.NetworkConfig.PlayerPrefab, spawnPoints[players.Count].position, Quaternion.identity);
NetworkObject netObj = playerObj.GetComponent<NetworkObject>();
netObj.SpawnAsPlayerObject(clientId);
LilBoiMovement playerScript = playerObj.GetComponent<LilBoiMovement>();
players.Add(playerScript);
playerScores[clientId] = 0;
}
private void StartNewRound()
{
Debug.Log("Starting a new round...");
roundActive.Value = true;
foreach (LilBoiMovement player in players)
{
if (player != null)
{
Debug.Log($"Reset request for Player {player.OwnerClientId}");
player.ResetPlayerStateClientRpc();
}
}
}
public void PlayerKnockedOut(ulong knockedOutId, ulong attackerId)
{
if (!IsOwner) return;
Debug.Log($"Player {knockedOutId} was knocked out by Player {attackerId}");
foreach (LilBoiMovement player in players)
{
if (player.OwnerClientId == knockedOutId)
{
player.hasFallen.Value = true;
Debug.Log($"Marked Player {knockedOutId} as fallen.");
break;
}
}
if (playerScores.ContainsKey(attackerId))
{
playerScores[attackerId]++;
Debug.Log($"Player {attackerId} now has {playerScores[attackerId]} points.");
if (playerScores[attackerId] >= maxPoints)
{
Debug.Log($"Player {attackerId} wins the game!");
EndGame(attackerId);
return;
}
}
int remaining = RemainingPlayers();
Debug.Log($"Players left: {remaining}");
if (remaining == 1)
{
Debug.Log("Only one player left, starting a new round...");
Invoke(nameof(StartNewRound), 3f);
}
}
private int RemainingPlayers()
{
int count = 0;
foreach (LilBoiMovement player in players)
{
if (player != null && player.health.Value > 0 && !player.hasFallen.Value)
{
count++;
}
}
Debug.Log($"RemainingPlayers() calculated: {count} players left.");
return count;
}
private void EndGame(ulong winnerId)
{
Debug.Log($"Player {winnerId} wins the game! Returning to profile screen.");
SceneManager.LoadScene("ProfileScreen");
}
}
Basically, the problem is that players spawn correctly, but nothing happens when characters die, it just doesn’t seem to detect it at all. No other logs are given either after a round has started. Any help is great.
Alex