After all this code runs and the scenes are loaded, my two characters can see each other, interact, animations are being shown on both sides.
My issue is that the script that holds all the variables, as written above, doesn’t allow the client to modify them. I can modify the variables for both player models on the host side but the client side remains read only.
This originally made me think that the character wasn’t being considered the ‘owner’ of the script but I have a conditional that won’t run unless ‘IsOwner and !IsServer’ and it executes without issue. Maybe I’m not understanding the Network variable properly?
The player is being instantiated from a prefab into the level scene on load.
Everyone has read permission by default. You only need to change write permission from server to owner.
Why do you control this with a bool in Update?
You can have an event for this, or if you want to trigger this with an Inspector bool for testing put the check in OnValidate.
The script with the variables is on the player object? Do you get any permission errors logged to the console? Does the client even execute the code that changes a netvar? (hint: debug)
This should actually leave the host unable to perform the code within the conditional because even if the host is the owner the host also happens to be the server, so the conditional is always false for the host.
Ah, I didn’t know that. Good to know for future network scripting.
This was simply to meet the conditions to start the game as a client from the cloned project so I could just tick it off in the inspector before clicking play. Like a debug menu.
The script with the variables is on the ‘Player’ prefab and it gets instantiated when the scene for the main game loads(after menu). So one is created on the host side and then one gets created when I join the host as the client.
There are no permission errors that have popped up in the Unity console.
As far as I can tell the client would have to be the one changing the variables on the netvar since they instantiate the player into the scene. If I remove the code that sets the clients netvar variables he will spawn with everything set to 0. Just to give context, the host player loads from a saved game file and gets all his attributes that way. The client doesn’t even reach that menu and will immediately grab the values I have set within the code and will only reach the function to do so if they are the client.
Sorry, I should have been clearer. This section of code is specifically run by the client and it does end up executing which is confusing. This is actually where the netvar values are set for the client player.
That seems to tell me the ownership is being applied appropriately.
The other weird thing is that I can edit the stats of my character, on the host side for the host character, as expected, but even though the input boxes are only being displayed on the host side for the client player model, I can’t actually change the values. Anytime I set a new value it resets back to the default which tells me the network variable properties are working as expected.
Is there a setting or something I’m missing that doesn’t allow the input boxes to be displayed on clones projects? I should clarify that I’m using ParrelSync to create the cloned project.
At this point this seems like intended behaviour. Do I have the dumb?
The below code works and let’s the host and client change their respective Player values. There is some strange behaviour when trying to change the values in the inspector, I’m not sure what’s going on there.