(Using Unity Netcode for Gameobject v2.1.1)
Let’s say for example I want to make a button in the game that all players in a lobby can click, every time you click it, the score will increase by 1, and all players will share this same score, so like a clicker game but co-op I guess…
So I initially I tried to just have an int value on the server and then from the client increase it, so I tried using NetworkVariables to keep that synced number, but it turns out you cannot set it so it can be increased by clients.
So then I tried to make a ServerRpc that clients can call to have the server increase the number, but then I got a KeyNotFoundException, which I figured out was basically since I didn’t have a NetworkObject that is in the Network Prefab List and is spawned.
So I made the Start() void tell the server to spawn the NetworkObject, and from what I understood that was supposed to spawn it on all clients in the lobby, including the host and other non-host clients. This did spawn on the host and it worked fine, but when I joined on another client it wouldn’t be spawned.
This was probably because the new client joined AFTER the server spawned the NetworkObject, so I tried to basically make a new void that’d spawn the NetworkObject and use InvokeRepeating() to call it like every 5 seconds.
Now, on the Host, this spawned the NetworkObject properly but would then give errors every 5 seconds about the NetworkObject already being spawned and server trying to spawn it again, but this wasn’t disturbing the gameplay. However, on the Client the NetworkObject for some reason still wouldn’t be spawned ever. I waited for more than 5 seconds and nothing happened, and if I remember correctly no error showed up.
Now this might be expected or this might be me poorly setting up Hosting/Joining lobbies in which case I will try to work on that script, but I would like to know if this sort of behaviour is expected or not? Does the Server spawn the NetworkObject on all Clients by default and if not is it possible to make it do that?
I know that it’s probably unsafe to even want to make something like this and there are probably safer methods but this is an example question and I’m just trying to understand how this stuff works and if what I wanted to accomplish is even possible.