How do I make the syncvariable have the same value on both the client and the host?,How do i make the SyncVariable have the same value on both the client and the host (client +server)?
I know this is a simple problem, but I can't get the sync variable isbrown to have the same value on both the client and host (client + server). Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class Player : NetworkBehaviour
{
[SyncVar]
bool isbrown = true;
[Command]
public void CmdChange()
{
isbrown = !isbrown;
}
void Update ()
{
Debug.Log(isbrown); // Every frame the debug log shows if the
// isbrown variable is true or false
if (Input.GetKeyDown(KeyCode.Space)) // if space is pressed, the command
{ // is called to change the variable.
CmdChange();
}
}
}
Whenever I press space on the host (client + server), the program works and the variable's value is changed to the opposite value. However, when I press space on the client, the debug log shows that the value of is brown is alternating between true and false. If I then press space on the client again, the debug log returns to showing only one value for is brown.
I'm not very experienced in networking, especially UNet, so I would greatly appreciate your help.
Your answer
Follow this Question
Related Questions
Unet NetworkServer.spawn, correcting my workflow: Found no behaviour for incoming Command 1 Answer
Networking functions work fine on player object, but not other objects 0 Answers
Looking for tips on identifying reason behind random client disconnects 0 Answers
non-player object can't execute's "[Command]"'s attributes 0 Answers
Best way to learn Networking in general 0 Answers