Null reference exception when accesing player

I get this warning but didn’t we set ConnectionApproval to true in an earlier lecture?

Warning: [Netcode] A ConnectionApproval callback is defined but ConnectionApproval is disabled. In order to use ConnectionApproval it has to be explicitly enabled

Hi Zoki,

I am guessing you got this resolved?
I apologize that you did not get a reply as our tagging system was not working at the time of posting so it did not come up in my daily searches.

If you can let us know the lecture we would be grateful so we can tag it correctly :slight_smile:

Hope that this is now resolved

Hey Marc,

Yeah I resolved it, I also reported the tagging issue to support, how to reproduce and which lessons lose the tags included. What was the original lesson where I noticed that is hard to tell, I do believe it was “Shutting down cleanly” (16_CO_UMP) but the actual issue was somewhere earlier.
Here’s one for 9_ga_ump that still has broken tags:

NullReferenceException: Object reference not set to an instance of an object
Leaderboard.HandlePlayerSpawned (TankPlayer player) (at Assets/Scripts/UI/Leaderboard/Leaderboard.cs:113)
Leaderboard.OnNetworkSpawn () (at Assets/Scripts/UI/Leaderboard/Leaderboard.cs:43)
Unity.Netcode.NetworkBehaviour.VisibleOnNetworkSpawn () (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.1/Runtime/Core/NetworkBehaviour.cs:497)
UnityEngine.Debug:LogException(Exception)
Unity.Netcode.NetworkBehaviour:VisibleOnNetworkSpawn() (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.1/Runtime/Core/NetworkBehaviour.cs:501)
Unity.Netcode.NetworkObject:InvokeBehaviourNetworkSpawn() (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.1/Runtime/Core/NetworkObject.cs:1066)
Unity.Netcode.NetworkSpawnManager:SpawnNetworkObjectLocallyCommon(NetworkObject, UInt64, Boolean, Boolean, UInt64, Boolean) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.1/Runtime/Spawning/NetworkSpawnManager.cs:636)
Unity.Netcode.NetworkSpawnManager:SpawnNetworkObjectLocally(NetworkObject, UInt64, Boolean, Boolean, UInt64, Boolean) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.1/Runtime/Spawning/NetworkSpawnManager.cs:539)
Unity.Netcode.NetworkSceneManager:OnServerLoadedScene(UInt32, Scene) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.1/Runtime/SceneManagement/NetworkSceneManager.cs:1630)
Unity.Netcode.NetworkSceneManager:OnSceneLoaded(UInt32) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.1/Runtime/SceneManagement/NetworkSceneManager.cs:1600)
Unity.Netcode.SceneEventProgress:<SetAsyncOperation>b__37_0(AsyncOperation) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.5.1/Runtime/SceneManagement/SceneEventProgress.cs:262)
UnityEngine.AsyncOperation:InvokeCompletionEvent()

I assume it tells the player is null, which is weird considering in the prior lesson it worked normally.
Lesson is Leaderboard Names, prior one is Leaderboard Spawning, but I just got there and haven’t done much debugging to begin with.

Unrelated question:
Which days/hours is support available? You’ve been a great help on this project (had a happy emoji but it turns into the creepy :slight_smile: so just imagine a happy face)

When a course is first being created i review the content and support the course for about the first month of its release.
We usually check support once a day and i do this first thing in the morning/after the school run so anytime between 9-10am GMT.
I sometimes spot check during the day as i am between reviewing videos or because i notice a notification.

Once that month period ends it passes over to another TA so i am free in case i need to cover any of our courses due to holiday or illness so then its down to when the assigned TA can check it.

There is no real set hours but we do respond typically within 24-48 hours

The error is saying for some reason the player (Being an object with the TankPlayer script) is null so its getting an empty reference.
You could try debugging out to see if it is getting the reference at all but i suspect it is never set.

Okay so in the function

	private void HandlePlayerSpawned(TankPlayer player)
	{
		leaderboardEntities.Add(new LeaderboardEntityState
		{
			ClientId = player.OwnerClientId,
			PlayerName = player.PlayerName.Value,
			Coins = 0
        });
        
		player.Wallet.TotalCoins.OnValueChanged += (oldCoins, newCoins) =>
			HandleCoinsChanged(player.OwnerClientId, newCoins);

	}

the player. has no reference set.
This function is called by

	public override void OnNetworkSpawn()
	{
		if (IsClient)
		{
			leaderboardEntities.OnListChanged += HandleLeaderboardEntitiesChanged;
			foreach(LeaderboardEntityState entity in leaderboardEntities)
			{
				HandleLeaderboardEntitiesChanged(new NetworkListEvent<LeaderboardEntityState>
				{
					Type = NetworkListEvent<LeaderboardEntityState>.EventType.Add,
					Value = entity
                });
			}
		}

		if (IsServer)
		{
			TankPlayer[] players = FindObjectsByType<TankPlayer>(FindObjectsSortMode.None);
			foreach (TankPlayer player in players)
			{
				HandlePlayerSpawned(player);
			}

			TankPlayer.OnPlayerSpawned += HandlePlayerSpawned;
			TankPlayer.OnPlayerDespawned += HandlePlayerDespawned;
		}
	}

so the issue is in

TankPlayer[] players = FindObjectsByType<TankPlayer>(FindObjectsSortMode.None);

I just need to figure out what’s going on and why it’s not finding any players

Edit: The player isn’t null, tried with debug.log and it returns okay results. Something else is the problem.

		player.Wallet.TotalCoins.OnValueChanged += (oldCoins, newCoins) =>
			HandleCoinsChanged(player.OwnerClientId, newCoins);

Something in here is wrong

Hi Zoki,

I’m currently away on vacation and the net connection is pretty abysmal so its hampering my ability to help a little here.
I’m going to ping our Unity Mirror mutiplayer course TA as he will eventually be taking over support of this course in the future and may be able to help you more effectively than i can at the moment (I get back on the 21st)

My initial instinct is that if it does not happy every single run then it could be a race condition in that the reference has not been got before its being called. It could still be the case if it occurs every time.
The code you have looks find to me when i use the github to check the code but i cant download any files to try and run some tests at the moment.

Yitzchak is very capable and will likely be able to help you further :wink:

@Yitzchak_Cohen Can you possibly take a look at this at some point please.

1 Like

All the best on vacation friend!
Haha I have some experience with abysmal connection speeds :'D
Just wanted to tell you I figured out what the problem is, sometimes when updating scripts on prefabs or prefabs themselves all the references and connection within the unity editor for that particular prefab get reset. There was one component that was out of place so I didn’t notice that I haven’t drag/droped its reference back in place.

Enjoy summer!

2 Likes

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

Privacy & Terms