Search Unity

[RESOLVED] SyncVars not working?

Discussion in 'Multiplayer' started by swapace, Oct 11, 2016.

  1. swapace


    Dec 20, 2015
    Hello everyone.
    Since yesterday I tried the Unity Networking System. I managed to create a small project, which uses a SyncVar to synchronize an integer across all clients. Everything worked fine.
    However, in a Project I started today, the SyncVars just don't seem to work. (i.e. they don't sync and don't call a hook on Clients, but they do on the host).
    I've tried a Struct containing a two-dimensional int Array, a string and an int. None of them works.

    The structure of the Code seems exactly the same:
    I set the value of the SyncVar in the NetworkManager in "OnStartServer". The Code first spawns an object using NetworkServer.Spawn and then sets the variable on a script that's on that prefab.
    The script with the prefab has the variable tagged with [SyncVar(hook = "PrintText")] and the PrintText method takes a string as an Argument and prints it. That method also gets called in OnStartClient to account for late-joining Clients.

    As stated before, the hook gets called on the host and prints the text just fine. On the Clients however, the hook does not get called, and when called manually by OnStartClient, the text is empty.

    Is there anything I could be doing wrong, or anything I'm not considering? (I have SyncVars working in a different Project, I can't seem to find any meaningful difference)

    Thanks in advance!

    EDIT: I fixed it. The Problem was with the NetworkServer.Spawn method, the variable I passed as a Parameter didn't contain the instantiated object but rather the prefab. That had to Change. After that the only Problem that was left was the two-dimensional Array in the struct. After syncing it, the Array was one-dimensional. I Changed the value to a one-dimensional Array and transformed the Array to work with it as an easy Workaround.
    Last edited: Oct 11, 2016