Search Unity

SyncEvent not reaching clients

Discussion in 'Multiplayer' started by ulvang, Nov 11, 2016.

  1. ulvang

    ulvang

    Joined:
    Sep 10, 2015
    Posts:
    6
    Hi.

    I am struggling to understand and make the SyncEvent functionality work. The idea is to have one class running only on the server, sending events to all clients.

    I have a class MyScenario, defining a SyncEvent:

    public class MyScenario : NetworkBehaviour {

    // set up network events
    public delegate void SetTimestep(int newTimestep);

    [SyncEvent]
    public event SetTimestep EventSetTimestep;


    and trigger this event in a timer:

    private IEnumerator MyTimer()
    {
    while(true)
    {
    yield return new WaitForSeconds(0.2f); // wait half a second
    // do things
    EventSetTimestep (m_time++);
    }
    }



    In another class I subscribe to this event in the Start function:

    public class MyData : NetworkBehaviour{

    void Start () {
    // subscribe to events
    if (myScenario) {
    myScenario.EventSetTimestep += setTimestep;
    }
    }


    This works perfect for local player on the host, but not for LAN clients ( I am testing this on only one PC running both HOST and LAN Client). The code for subscribing is called without errors, but the setTimestep method is never called in the LAN client.

    Both classes are based on NetworkBehaviour and have NetworkIdentity, but I do not understand completely how to setup the code so that MyScenario only runs on the server. Also the setting for the check-boxed on the NetworkIdentity (Server only / Local Player Authority) is not clear to me.
     
  2. ulvang

    ulvang

    Joined:
    Sep 10, 2015
    Posts:
    6
    In the example above, should I set the Network Identity of MyScenario be set to Server Only, or not?

    And does the MyData game object has to be created/instantiated as a network prefab run-time, or can it exist in the hierarchic as a static object?
     
    Last edited: Nov 18, 2016
  3. c2z4s9

    c2z4s9

    Joined:
    Apr 10, 2017
    Posts:
    10
    I'm having the exact same problem. In my case the MyData game object is the "player" (network prefab created on client connect) and it still doesn't work. I'm using this to get it started on the server side object:

    private void Start() {
    print("Starting to invoke repeating.");
    InvokeRepeating("TriggerEventAction", 0, 5);
    }

    and TriggerEventAction is tagged with [server]. Did you ever figure this out? It seems like the events just don't travel to the clients.