Skip to content

Commit 7a969f8

Browse files
authored
fix: Presence of NetworkVariable<T>[] in a class causes an exception (#2584)
fixes #2576
1 parent f69b6cb commit 7a969f8

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,9 +2225,9 @@ private void GenerateVariableInitialization(TypeDefinition type)
22252225
}
22262226
field = new FieldReference(fieldDefinition.Name, fieldDefinition.FieldType, genericType);
22272227
}
2228-
if (field.FieldType.IsSubclassOf(m_NetworkVariableBase_TypeRef))
2228+
if (!field.FieldType.IsArray && !field.FieldType.Resolve().IsArray && field.FieldType.IsSubclassOf(m_NetworkVariableBase_TypeRef))
22292229
{
2230-
// if({variable} != null) {
2230+
// if({variable} == null) {
22312231
processor.Emit(OpCodes.Ldarg_0);
22322232
processor.Emit(OpCodes.Ldfld, field);
22332233
processor.Emit(OpCodes.Ldnull);

com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariableTests.cs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,28 @@ public class NetworkVariableSubclass<TSubclassName> : NetworkVariableMiddleclass
2828

2929
}
3030

31+
public class NetworkBehaviourWithNetVarArray : NetworkBehaviour
32+
{
33+
public NetworkVariable<int> Int0 = new NetworkVariable<int>();
34+
public NetworkVariable<int> Int1 = new NetworkVariable<int>();
35+
public NetworkVariable<int> Int2 = new NetworkVariable<int>();
36+
public NetworkVariable<int> Int3 = new NetworkVariable<int>();
37+
public NetworkVariable<int> Int4 = new NetworkVariable<int>();
38+
public NetworkVariable<int>[] AllInts = new NetworkVariable<int>[5];
39+
40+
public int InitializedFieldCount => NetworkVariableFields.Count;
41+
42+
43+
private void Awake()
44+
{
45+
AllInts[0] = Int0;
46+
AllInts[1] = Int1;
47+
AllInts[2] = Int2;
48+
AllInts[3] = Int3;
49+
AllInts[4] = Int4;
50+
}
51+
}
52+
3153
public struct TemplatedValueOnlyReferencedByNetworkVariableSubclass<T> : INetworkSerializeByMemcpy
3254
where T : unmanaged
3355
{
@@ -1435,6 +1457,40 @@ public void TestUnsupportedUnmanagedTypesWithUserSerializationDoNotThrowExceptio
14351457
UserNetworkVariableSerialization<Guid>.DuplicateValue = null;
14361458
}
14371459
}
1460+
[Test]
1461+
public void WhenCreatingAnArrayOfNetVars_InitializingVariablesDoesNotThrowAnException()
1462+
{
1463+
var testObjPrefab = CreateNetworkObjectPrefab($"NetVarArrayPrefab");
1464+
var testComp = testObjPrefab.AddComponent<NetworkBehaviourWithNetVarArray>();
1465+
testComp.InitializeVariables();
1466+
1467+
// Verify all variables were initialized
1468+
Assert.AreEqual(testComp.InitializedFieldCount, 5);
1469+
1470+
Assert.NotNull(testComp.Int0.GetBehaviour());
1471+
Assert.NotNull(testComp.Int1.GetBehaviour());
1472+
Assert.NotNull(testComp.Int2.GetBehaviour());
1473+
Assert.NotNull(testComp.Int3.GetBehaviour());
1474+
Assert.NotNull(testComp.Int4.GetBehaviour());
1475+
1476+
Assert.NotNull(testComp.Int0.Name);
1477+
Assert.NotNull(testComp.Int1.Name);
1478+
Assert.NotNull(testComp.Int2.Name);
1479+
Assert.NotNull(testComp.Int3.Name);
1480+
Assert.NotNull(testComp.Int4.Name);
1481+
1482+
Assert.AreNotEqual("", testComp.Int0.Name);
1483+
Assert.AreNotEqual("", testComp.Int1.Name);
1484+
Assert.AreNotEqual("", testComp.Int2.Name);
1485+
Assert.AreNotEqual("", testComp.Int3.Name);
1486+
Assert.AreNotEqual("", testComp.Int4.Name);
1487+
1488+
Assert.AreSame(testComp.AllInts[0], testComp.Int0);
1489+
Assert.AreSame(testComp.AllInts[1], testComp.Int1);
1490+
Assert.AreSame(testComp.AllInts[2], testComp.Int2);
1491+
Assert.AreSame(testComp.AllInts[3], testComp.Int3);
1492+
Assert.AreSame(testComp.AllInts[4], testComp.Int4);
1493+
}
14381494

14391495
private void TestValueType<T>(T testValue, T changedValue) where T : unmanaged
14401496
{
@@ -1562,7 +1618,6 @@ private void TestValueTypeNativeList<T>(NativeList<T> testValue, NativeList<T> c
15621618
clientVariable.Dispose();
15631619
}
15641620
#endif
1565-
15661621
[Test]
15671622
public void WhenSerializingAndDeserializingValueTypeNetworkVariables_ValuesAreSerializedCorrectly(
15681623

0 commit comments

Comments
 (0)