Skip to content

SyncVars fail to initialize correctly on host after multiple objects are spawned. #4100

@Roshinator

Description

@Roshinator

Describe the bug
On a host, instantiate a prefab, spawn it, then assign the value of a non-primitive type syncvar on the spawned object. If you loop this process a few times, the syncvar starts losing the assigned value on new objects.

[IMPORTANT] How can we reproduce the issue, step by step:
Please tell us how to reproduce your issue, STEP BY STEP, with one of our built in examples.
-> Tell us every single click / key press so we can reproduce it
-> If it can't be reproduced with our built in examples, tell us what else to do
-> Explain like we are five

  1. Use the MirrorBasic example as a starting point.
  2. Create a simple cube object in the scene, assign it a network identity and attach this script to it:
using UnityEngine;
using Mirror;

public class CubeTest : NetworkBehaviour
{
    [SyncVar(hook = nameof(UpdateHook))]
    public string testValue;

    public void UpdateHook(string oldI, string newI)
    {
        Debug.Log($"Old: {oldI}, New: {newI}");
    }
}
  1. Save this object as a prefab to assets, remove it from the scene, and add it to the network manager's registered spawnable prefabs.
  2. Create an empty scene object and assign to it a network identity and this script:
using Mirror;
using UnityEngine;

public class SpawnerTest : NetworkBehaviour
{
    public GameObject prefab;
    int repeatCount = 10;
    public override void OnStartServer()
    {
        base.OnStartServer();
        repeatCount = 10;
    }

    void Update()
    {
        if (repeatCount > 0)
        {
            GameObject g = Instantiate(prefab);
            NetworkServer.Spawn(g);
            g.GetComponent<CubeTest>().testValue = "test string";

            repeatCount--;
        }
    }
}
  1. Assign the cube prefab to the SpawnerTest component in the editor.
  2. Run the scene and click host.
  3. Observe the console output for the UpdateHook's log statements. After the first few objects are spawned, the output should change. Example output from my system is below.
  4. If similar output isnt observed, maybe try increasing the repeatCount.
Image

Expected behavior
A clear and concise description of what you expected to happen.
All of these logs should consistently show "test string" as the value of the syncvar for both the old and new parameters.
CORRECTION: All of these logs should show blank for the old value and "test string" for the new value.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: Windows
  • Build target: Windows (observed in editor)
  • Unity version: 6000.3.10f1
  • Mirror branch: 96.9.19

Repro project, open the mirror/basic/scenes/MirrorBasic.unity scene.
Mirror 96.9.19 Bug.zip

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions