[Solved]NullReferenceException in a ClientRpc method

Hello there.

I’m trying to make a coop game and here is my issue.
I have two functions that I call to indicate if a player has equipped an item so that both players can see visually those items equipped each other characters.

Here is how I call those functions :

if(!isServer){
	CmdItemEquipped(eItems[itemName].GetComponentInParent<NetworkIdentity>().netId,eItems[itemName].name);
}else{
	RpcItemEquipped(eItems[itemName].GetComponentInParent<NetworkIdentity>().netId,eItems[itemName].name);
}

Basically if I am not the server I’m calling the CmdItemEquipped so that the server can see that I equipped an item and indicate it visually to itself and the other clients. That’s working.
The second part is if I’m the server; then I’m calling the RpcItemEquipped function that does the same thing as the first function but is sent to the clients to indicate them to visually equip a weapon on the server’s player.
But the second function does not completely work. Here are those two functions :

	[Command]
	void CmdItemEquipped(NetworkInstanceId playerID,string itemName){
		GameObject player = NetworkServer.FindLocalObject (playerID);
		Debug.Log (playerID + " found");
		Item[] items = player.GetComponentsInChildren<Item>(includeInactive:true);
		Debug.Log (items.Length+" items trouvés");
		foreach (Item item in items) {
			if(item.gameObject.name==itemName){
				item.gameObject.SetActive(true);
				Debug.Log(itemName+" activated");
				break;
			}
		}
	}

	[ClientRpc]
	void RpcItemEquipped(NetworkInstanceId playerID,string itemName){
		GameObject player = NetworkServer.FindLocalObject (playerID);
		Debug.Log (playerID + " found");
		Item[] items = player.GetComponentsInChildren<Item>(includeInactive:true);
		Debug.Log (items.Length+" items trouvés");
		foreach (Item item in items) {
			if(item.gameObject.name==itemName){
				item.gameObject.SetActive(true);
				Debug.Log(itemName+" activated");
				break;
			}
		}
	}

The first one works as intended. I pass the NetworkInstanceId as an argument to be able to retrieve the player’s gameobject then I pass a second argument which is the item name.
With those I’m able to find the player’s gameobject on the server then retrieve the item the player equipped and activate it so that it appears visually.

On the second function the player is found with the NetworkInstanceId the Debug.Log(playerID + “found”) confirmed it but the function can’t seem to be able to find the items on the server’s player gameobject and the client is unable to see visually what the server’s player just equipped.

Here is the error I get in the console :

NullReferenceException: Object reference not set to an instance of an object
PlayerAction.RpcItemEquipped (NetworkInstanceId playerID, System.String itemName) (at Assets/Scripts/PlayerAction.cs:150)

It points out to this line of code :

Item[] items = player.GetComponentsInChildren<Item>(includeInactive:true);

I can’t find what’s wrong with it since the first function works perfectly plus the player is found so the player variable is not null but when I try to get the items from it I get a NullReferenceException.

The weird thing is I saw that the items were there in the inspector when I’m starting Unity as the client.

If anyone as any hint or suggestion it would be a great leap forward for me.

Thanks.

I found the issue.

I am using the NetworkServer on the client which obviously now that I think about it is idiotic since NetworkServer is only present on… well… the server.

Now I need to find another way to tell the client that my server’s player changed weapon.

SOLUTION if anyone is wondering.
You can use ClientScene.FindLocalObject() to return a gameobject from a networkid.