My game crashes when the one who joins tries to click on their unitSpawner

I suddently got a lot of nullReferenceExceptions nad when I tried to spawn units from the one who joined the server the game just ends…
image

UnitSelectionHandler

BuildingButton

RescourceDisplay

I poked around and also discovered this error

Hi there,

It seem like the main issue is the repeated null reference errors, so let solve those first. We expect that the first time we attempt to get the player, sometimes the player hasn’t been spawned yet, so we get a null reference. However, sometimes the network connection isn’t even established, so we get a null reference error on the “connection” part.

The solution many students have used is to move the get player statement into a coroutine wherever this error is occurring and add a small delay.

private IEnumerator GetPlayer()
{
     if(player == null)
    {
         yield return new waitForSeconds(0.5f);
         player = NetworkClient.connection.identitiy.GetComponent<RTSPlayer>();
}

Then call this with StartCoroutine(GetPlayer()).

Thanks a lot Yitzchak_Cohen,

That solved the first problem with the null reference errors, then there is only the error with the one who joins the game where the game crashes when I press on the UnitSpawner

Is that the line where you are invoking the ClientResourcesUpdated?
Try adding the "?’ before the invoke:
ClientResourcesUpdated?.Invoke(newResources);
What this does is it only invokes the event if there is a subscriber. The non-authority clients have no subscribers so they will throw an error when that is invoked.

Thank you, I had completely missed it. :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms