Game Over only showing on Host/Client not the Client

Not sure what the issue is, have re checked my code comparing to the resources. If someone has any ideas or need me to post code let me know it would be much appreciated, Thanks.

Hi there, sorry you are having trouble. It would be helpful if you could share your gameOverHandler code here.
Is the problem occurring every time the game ends, even the first time?
Are there any errors in the console?

Only shows on Server/Client window no matter who wins, displays proper player as winner.

I get 1 error if I run the game in the unity editor, I have been doing build / run to test.

Using 2020.3.23f1 Unity ver.

NullReferenceException: Object reference not set to an instance of an object
UnitSelectionHandler.Update () (at Assets/Scripts/Units/UnitSelectionHandler.cs:38)

which points to this line

player = NetworkClient.connection.identity.GetComponent();

using System;
using Mirror;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameOverHandler : NetworkBehaviour
{
public static event Action ClientOnGameOver;

private List<UnitBase> bases = new List<UnitBase>();

#region Server

public override void OnStartServer()
{
    UnitBase.ServerOnBaseSpawned += ServerHandleBaseSpawn;
    UnitBase.ServerOnBaseDespawned += ServerHandleBaseDespawn;
}

public override void OnStopServer()
{
    UnitBase.ServerOnBaseSpawned -= ServerHandleBaseSpawn;
    UnitBase.ServerOnBaseDespawned -= ServerHandleBaseDespawn;
}

[Server]
private void ServerHandleBaseSpawn(UnitBase unitBase)
{
    bases.Add(unitBase);
}

[Server]
private void ServerHandleBaseDespawn(UnitBase unitBase)
{
    bases.Remove(unitBase);

    if (bases.Count != 1)
    { return; }

    int playerId = bases[0].connectionToClient.connectionId;

    RpcGameOver($"Player {playerId}");
}

#endregion

#region Client

[ClientRpc]
private void RpcGameOver(string winner)
{
    ClientOnGameOver?.Invoke(winner);
}

#endregion

}

This null error could be breaking the game, and then causing the later error in some way. Is this happening when the game starts?

Try adding the line causing the null reference error to a coroutine with a half a second delay. I have seen many cases where the the network connection was null and trying to get the player too soon caused an error. Adding a small delay could fix this issue.

Let me know if that works, and then we can go from there.

Thank You for your time, will try this but might be a day or so before I am able. I will post back the results. Again Thanks.

1 Like

Make sure you’ve added your “GameOverHandler” prefab to the list of spawnable objects on the list in your NetworkManager. It needs to be able to spawn it on the clients as well, for the RPC to be available. Also, make sure your RPC method name starts with “Rpc” and has the “[ClientRpc]” attribute before it.

If that’s not the issue, you might be able to get more information by hosting in the built executable and connecting as client from the Unity editor, so that you can see what errors the client-only is getting.

1 Like

Privacy & Terms