Search Unity

Updating Sync variable in one script from another player script not syncing on host?

Discussion in 'Multiplayer' started by Dreamaster, Jul 27, 2015.

  1. Dreamaster

    Dreamaster

    Joined:
    Aug 4, 2014
    Posts:
    148
    So basically... I have a Controller script that is only active on one player at a time, that calls a setter on a "global script" to change a boolean syncvar, and it works fine when the CLIENT player makes the call, but when the HOST makes this same call it's as though the SyncVar doesn't ever register that it's value has been changed and the Debug Log never shows. Could this be a UNET bug or am I missing something?

    Something like:

    GlobalVars.cs
    [SyncVar Onchange="UpdateClientEngineBool"] public bool EngineOnOff = false;

    void UpdateClientEngineBool(bool EngineServerBoolValue)
    {
    EngineOnOff = EngineServerBoolValue;
    }
    [Command]
    public void CmdSetEngineBoolOnServer(bool EngineBool)
    {
    EngineOnOff = EngineBool;
    Debug.Log("Setting value");
    }

    Then from the script attached to the playerobject:
    Controller.cs
    private bool EnginePlayerBool = false;
    public GameObject GameManager;
    if(!isPlayer)
    {
    if(Input.GetKeyCode(KeyCode.L)
    {
    EnginePlayerBool = true;
    GameManager.GetComponent<GlobalVars>().CmdSetEngineBoolOnServer(EnginePlayerBool);
    }
    }
     
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    Dreamaster likes this.
  3. Dreamaster

    Dreamaster

    Joined:
    Aug 4, 2014
    Posts:
    148
    OMG!! That led me to the right code, thanks. So what I wound up doing was creating the command on the player script that would call the setter in the globalvar script.

    This is really cool, that boolean is controlling helicopter rotor animations... I'm starting to think making a network coop game could be a reality for me! :)