recentlly, i was tring to synchronize struct data from host to client with SyncVar. but it failed.
however, the data of int type can be synchronized by the SyncVar.
So, i do not know Does it has any differece between int and struct data when i synchronized with syncvar tag.
i have uploaded my code, Can anyone tell me where was wrong and give me some advices.
thanks very much!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class MyDataTransmition : NetworkBehaviour {
bool myFlag=false;
[System.Serializable]
public struct myDataStruct{
public int myNum;
public float mySpeed;
public string myName;
}
[SyncVar]
public int i=0;
[SyncVar]
public myDataStruct myData=new myDataStruct();
// Use this for initialization
void Start () {
//myData = new myDataStruct ();
I try this method, it works.but can i just use syncvar like int type if i want synchronize a single struct.
i find that int data can be synchronized just by adding syncvar tag before it
Changing an individual value within a SyncVar struct does not set it to dirty, so does not sync to clients. Break out the individual variables within the struct to their own SyncVars or replace the entire struct to get it to sync. I believe SyncListStruct has the same limitation.
Though it is obviously not very efficient, so I’d just use individual SyncVar basic types instead of structs for this.
Use SyncVar structs or SyncListStructs for data that you need to send to clients, but individual values aren’t going to change from frame to frame. For example, I use them for a server chat system where I populate a SyncListStruct with chat messages. Individual chat messages don’t get changes to them after they are sent (you don’t rewrite your message or change who the message is from), they are just eventually dropped from the list and replaced by new chat messages.