Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

[SyncVar] doesn't work??

Discussion in 'Multiplayer' started by Royall, Jul 1, 2015.

  1. Royall

    Royall

    Joined:
    Jun 15, 2013
    Posts:
    123
    I have a server/client setup where the server spawns some NPC's with
    Code (CSharp):
    1. NetworkServer.Spawn(obj);
    In the enemy controller script I have
    Code (CSharp):
    1. public override bool OnSerialize(NetworkWriter writer, bool initialState) {
    2.         //Debug.Log("Send enemy update");
    3.         writer.Write(transform.position);
    4.         writer.Write(transform.rotation);
    5.         return true;
    6.     }
    7.     public override void OnDeserialize(NetworkReader reader, bool initialState) {
    8.         //Debug.Log("Receive enemy update");
    9.         enemyNewPos = reader.ReadVector3();
    10.         enemyNewRot =  reader.ReadQuaternion();
    11.     }
    to sync some positions, this all works fine if I have SetDirtyBit(1u); in the Update function (Dunno if this is correct).

    Problem is now I want to sync the name of the NPC. The name of the NPC works on the server, but it won't sync to the clients with:
    Code (CSharp):
    1. [SyncVar]
    2.     public string enemyName;
    The string stays empty when I look at the NPC on the client...

    Does anyone know whats the problem here?
     
  2. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    938
    You can not use SyncVar and overridden Serialize function simultaneously (see the 'Custom Serialization Functions' section)
    . Either add the serialization code for the name to the functions, or split the code in multiple components (which, to be honest, seems like a good idea anyway judging from the information you provided)