Answer the question
In order to leave comments, you need to log in
How to sync data from client to host using Mirror Network in Unity?
When exploring Mirror, I ran into the problem that the host transmits data to clients, and the clients do not transmit data to the server.
I implemented the transfer through "SyncVar":
public class MirrorTest : NetworkBehaviour
{
[SyncVar(hook = nameof(SetColor))]
public Color playerColor = Color.black;
public SpriteRenderer playerSprite;
public Color[] colors;
private void Update()
{
if (isLocalPlayer)
{
if (Input.GetKeyDown(KeyCode.Space))
{
int randomColor = Random.Range(0, colors.Length);
playerColor = colors[randomColor];
}
}
}
void SetColor(Color oldColor, Color newColor)
{
playerSprite.color = newColor;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question