SyncVars of Structs Do not Serialize NetworkBehaviours for Late Joiners.
General Unity version: 2021.3.17 Fish-Networking version: 3.7.7
Description When attempting to serialize a custom struct using the sync var attribute, if the syncvar has a networkbehaviour or something that extends networkbehaviour, that variable will not be synced for late joiners (field will be null). This issue appears to have originated sometime after 3.6.9 (we're upgrading from 3.5.8).
Replication Steps to reproduce the behavior:
- Attach this script to a gameObject. https://pastebin.com/xyMTd1wR
- Start a host/client match, modify the 'behaviour' field of the struct syncvar somehow.
- Join with another client.
- Inspect the gameObject, see that the 'behaviour' field is null.
Expected behavior The networkbehaviour is not null and is useable.
Bump. Observed similar problematic behavior in 3.7.10 Pro.
Bump again, am I doing something wrong? I would really like to get some of the performance benefits of the newer fishnet.
Didn't forget this is just a low priority atm, sorry.
I'm going to review this issue after 3.11.0; ideally the release after.
Status updated to priority.
Unable to reproduce.
Setup:
- Add NetworkManager prefab, ensure auto start is Server or disabled.
- Add a scene object and attach StructureSV script (see below).
- Make a new prefab and add EmptyNetworkBehaviour. Set as OtherPrefab in added StructureSV component.
- Start one editor as server, and one as client.
public class StructureSV : NetworkBehaviour
{
public NetworkObject OtherPrefab;
public struct MyData
{
public bool Set;
public NetworkObject Nob;
public NetworkBehaviour Nb;
}
[SyncVar]
private MyData _serverStartMd;
[SyncVar]
private MyData _spawnMd;
public override void OnStartServer()
{
NetworkObject n = Instantiate(OtherPrefab);
base.Spawn(n);
_serverStartMd = new MyData()
{
Set = true,
Nob = n,
Nb = n.GetComponent<NetworkBehaviour>()
};
}
public override void OnSpawnServer(NetworkConnection connection)
{
NetworkObject n = Instantiate(OtherPrefab);
base.Spawn(n);
_spawnMd = new MyData()
{
Set = true,
Nob = n,
Nb = n.GetComponent<NetworkBehaviour>()
};
}
public override void OnStartClient()
{
StartCoroutine(__PrintMD(0f));
StartCoroutine(__PrintMD(0.5f));
}
private IEnumerator __PrintMD(float delay)
{
if (delay > 0f)
yield return new WaitForSeconds(delay);
Debug.Log($"ServerStartMd Check: Delay {delay}. Set {_serverStartMd.Set}. Nob null? {_serverStartMd.Nob == null}. Nb null? {_serverStartMd.Nb == null}.");
Debug.Log($"SpawnMd Check: Delay {delay}. Set {_spawnMd.Set}. Nob null? {_spawnMd.Nob == null}. Nb null? {_spawnMd.Nb == null}.");
}
}
Expected behavior: When client starts ServerStartMd will be set and Nob/Nb will not be null. This is expected because the values were set before the object was spawned in for the client. When client starts SpawnMd will be unset and have null Nob/Nb until the delay of 0.5f(as shown in script) passes. This is expected because the values are set in OnServerSpawn, so they are sent after the client calls OnStartClient. In result the client gets the values later and should show set after the 0.5f delay.
See screenshot for local results.
Closing issue until I receive a test project / further information. Please re-open at that time.