diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index 423096ac35..43b381c876 100644 --- a/com.unity.netcode.gameobjects/CHANGELOG.md +++ b/com.unity.netcode.gameobjects/CHANGELOG.md @@ -12,6 +12,8 @@ Additional documentation and release notes are available at [Multiplayer Documen ### Fixed +- Fixed issue where `NetworkVariableBase` derived classes were not being re-initialized if the associated `NetworkObject` instance was not destroyed and respawned. (#3181) + ### Changed diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs index adc7ec0fa4..6b4c3585c3 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs @@ -918,10 +918,25 @@ internal void __nameNetworkVariable(NetworkVariableBase variable, string varName variable.Name = varName; } + /// + /// Does a first pass initialization for RPCs and NetworkVariables + /// If already initialized, then it just re-initializes the NetworkVariables. + /// internal void InitializeVariables() { if (m_VarInit) { + // If the primary initialization has already been done, then go ahead + // and re-initialize each NetworkVariable in the event it is an in-scene + // placed NetworkObject in an already loaded scene that has already been + // used within a network session =or= if this is a pooled NetworkObject + // that is being repurposed. + for (int i = 0; i < NetworkVariableFields.Count; i++) + { + NetworkVariableFields[i].Initialize(this); + } + // Exit early as we don't need to run through the rest of this initialization + // process return; } diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariable/NetworkVariableAnticipationTests.cs b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariable/NetworkVariableAnticipationTests.cs index c436275fea..f73b42484a 100644 --- a/com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariable/NetworkVariableAnticipationTests.cs +++ b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariable/NetworkVariableAnticipationTests.cs @@ -248,25 +248,25 @@ public void WhenServerChangesSmoothValue_ValuesAreLerped() WaitForMessageReceivedWithTimeTravel(m_ClientNetworkManagers.ToList()); - Assert.AreEqual(15 + 1f / 60f * 5, testComponent.SmoothOnAnticipationFailVariable.Value, Mathf.Epsilon); + Assert.AreEqual(15 + 1f / 60f * 15, testComponent.SmoothOnAnticipationFailVariable.Value, Mathf.Epsilon); Assert.AreEqual(20, testComponent.SmoothOnAnticipationFailVariable.AuthoritativeValue, Mathf.Epsilon); - Assert.AreEqual(0 + 1f / 60f * 20, otherClientComponent.SmoothOnAnticipationFailVariable.Value, Mathf.Epsilon); + Assert.AreEqual(1f, otherClientComponent.SmoothOnAnticipationFailVariable.Value, 0.000015f); Assert.AreEqual(20, otherClientComponent.SmoothOnAnticipationFailVariable.AuthoritativeValue, Mathf.Epsilon); - for (var i = 1; i < 60; ++i) + for (var i = 1; i < 15; ++i) { TimeTravel(1f / 60f, 1); - Assert.AreEqual(15 + 1f / 60f * 5 * (i + 1), testComponent.SmoothOnAnticipationFailVariable.Value, 0.00001); + Assert.AreEqual(15 + 1f / 60f * 15 * (i + 1), testComponent.SmoothOnAnticipationFailVariable.Value, 0.00001); Assert.AreEqual(20, testComponent.SmoothOnAnticipationFailVariable.AuthoritativeValue, Mathf.Epsilon); - Assert.AreEqual(0 + 1f / 60f * 20 * (i + 1), otherClientComponent.SmoothOnAnticipationFailVariable.Value, 0.00001); + Assert.AreEqual(1.0f + i, otherClientComponent.SmoothOnAnticipationFailVariable.Value, 0.00001); Assert.AreEqual(20, otherClientComponent.SmoothOnAnticipationFailVariable.AuthoritativeValue, Mathf.Epsilon); } TimeTravel(1f / 60f, 1); - Assert.AreEqual(20, testComponent.SmoothOnAnticipationFailVariable.Value, Mathf.Epsilon); - Assert.AreEqual(20, otherClientComponent.SmoothOnAnticipationFailVariable.Value, Mathf.Epsilon); + Assert.AreEqual(19, testComponent.SmoothOnAnticipationFailVariable.Value, Mathf.Epsilon); + Assert.AreEqual(16, otherClientComponent.SmoothOnAnticipationFailVariable.Value, 0.00001); } [Test]