I want to make a multiplayer card game for a school project. The game has 6 turns and both players have to press “Ready” in order for the game to advance to the next turn (like Marvel Snap).
Both players have a NetworkVariable attached to their prefab which becomes true after the button is pressed and the button becomes uninteractable until the start of the next turn.
I had a lot of problems when trying to implement this so I made a simple test scene with just a button where all I wanted was to have the button increment a NetworkVariable if the button was pressed by the host and decrement if it was pressed by the client.
The problem is that it doesn’t update on either side and the NetworkObject component that’s on the GameObject with the script for testing isn’t spawned on the Host’s side by default. I have to spawn it in the Inspector or via code and from what I understood from the documentation it should have spawned by default (it does for the other GameObjects and it is in the Network Manager prefab list like the others). This GameObject and the button are spawned in the scene, before (?) the players join.
This is what it looks after I spawn the NetworkObject component on the Host:

This is what it looks on the Client:
(I get 4 errors with the default values)

Here is the code I initially tested with:
The button’s onClick() is assigned the pressedBy() function.
using Unity.Netcode;
using UnityEngine;
public class ReadyButton : NetworkBehaviour
{
[SerializeField]
public NetworkVariable<int>test; //test integer
[ServerRpc(RequireOwnership =false)]
public void RpcExampleServerRpc(int type)
{
if (type==1)
{
Debug.Log("ServerRpc called by the host!!");
test.Value++;
}
else
{
Debug.Log("ServerRpc called by the client!");
test.Value--;
}
}
public void presedBy()
{
//initially tried with a ServerRpc but it only worked when called by the Host (and only incremented on the Host's side)
//couldn't make the Client's side work since it has to be on the Server
if(NetworkManager.IsServer)
{
test.Value++;
//RpcExampleServerRpc(1);
}
else
{
//this was a little different initially, if RpcExampleServerRpc(2) ran like it is now it would give a missing Dictionary error
test.Value--;
//RpcExampleServerRpc(2);
}
}
void Start()
{
if(NetworkManager.IsServer)
{
//Spawning the Host's NetworkObject Component
this.GetComponent<NetworkObject>().Spawn();
test.Value=0;
}
}
}
It didn’t work so I tried this code (which also doesn’t work):
The button’s onClick() is assigned the onPress() function.
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
public class NetVar : NetworkBehaviour
{
[SerializeField]
public NetworkVariable<int> number;
public void onPress()
{
number.Value++;
}
}
At this point I’m lost. I don’t know what else to test or change to make the button work. I didn’t have these problems when I made a ServerRpc button in the menu (which is the first scene) to test its functionality.

