Search Unity

Unet : SyncVar Not Syncing! Tried everything

Discussion in 'UNet' started by lullaharsh, Sep 23, 2017.

  1. lullaharsh

    lullaharsh

    Joined:
    Sep 9, 2012
    Posts:
    29
    I have tried everything I could and wasted a lot of time on this very worthless topic but I still dont get why this doesnt work...... The client updates the value and it does not sync to the editor (who is hosting) where the value is still the default one (-1).

    Code (CSharp):
    1. [SyncVar]
    2.     public int survivorID = -1; //Starts from 1...
    3.  
    4. public override void OnStartLocalPlayer()
    5.     {
    6.         if (!isLocalPlayer)
    7.         {
    8.             return;
    9.         }
    10.          killer = FindObjectOfType<Killer>();
    11.         StartCoroutine(SettingSurvIDs());
    12.     }
    13.  
    14. IEnumerator SettingSurvIDs()
    15.     {
    16.         yield return new WaitForSeconds(0.3f);
    17.         CmdSetSurvivorID();
    18.     }
    19.  
    20.     [Command]
    21.     void CmdSetSurvivorID()
    22.     {
    23.         survivorID = int.Parse(netId.ToString()) - (int.Parse(killer.numberOfSurvivors.ToString())+2);
    24.     }
    EDIT : I am well aware that I don't need to add the islocalplayer check there but as I said I'm trying everything that I can
     
  2. TheWesselGames

    TheWesselGames

    Joined:
    Aug 31, 2016
    Posts:
    31
    Syncvars only sync from server --> client, client --> server will not work.

    Read the documentation carfully next time.
     
  3. Deleted User

    Deleted User

    Guest

    He called a Command so it's fine.
     
  4. Deleted User

    Deleted User

    Guest

    I guess the reason is OnStartLocalPlayer() try to change it to Start() but check for if(isLocalPlayer);
     
  5. lullaharsh

    lullaharsh

    Joined:
    Sep 9, 2012
    Posts:
    29
    @TheWesselGames I don't mean to be rude but please at least read the code when I'm saying I have tried everything before commenting that.
     
    Deleted User likes this.
  6. lullaharsh

    lullaharsh

    Joined:
    Sep 9, 2012
    Posts:
    29
    Unfortunately that doesnt work :(. I even tried putting it in update but still doesnt work.
     
  7. lullaharsh

    lullaharsh

    Joined:
    Sep 9, 2012
    Posts:
    29
    Fixed it! The problem was not with the SyncVar itself. It was with the killer object not being assigned on the server properly. It wasn't even throwing any errors until I decided to debug the thing i was setting the survivorID to... It works fine now! Thanks for your time.
     
    Deleted User likes this.