Skip to content

Commit f1527c7

Browse files
committed
style: simplify null checks
1 parent 2a6f4cb commit f1527c7

File tree

5 files changed

+6
-38
lines changed

5 files changed

+6
-38
lines changed

com.unity.netcode.gameobjects/Editor/NetworkManagerEditor.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,7 @@ public override void OnInspectorGUI()
231231
{
232232
ReloadTransports();
233233

234-
var transportComponent = m_NetworkManager.gameObject.GetComponent(m_TransportTypes[selection - 1]);
235-
236-
if (transportComponent == null)
237-
{
238-
transportComponent = m_NetworkManager.gameObject.AddComponent(m_TransportTypes[selection - 1]);
239-
}
240-
234+
var transportComponent = m_NetworkManager.gameObject.GetComponent(m_TransportTypes[selection - 1]) ?? m_NetworkManager.gameObject.AddComponent(m_TransportTypes[selection - 1]);
241235
m_NetworkTransportProperty.objectReferenceValue = transportComponent;
242236

243237
Repaint();

com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -597,13 +597,7 @@ internal void InitializeVariables()
597597
var fieldType = sortedFields[i].FieldType;
598598
if (fieldType.IsSubclassOf(typeof(NetworkVariableBase)))
599599
{
600-
var instance = (NetworkVariableBase)sortedFields[i].GetValue(this);
601-
602-
if (instance == null)
603-
{
604-
throw new Exception($"{GetType().FullName}.{sortedFields[i].Name} cannot be null. All {nameof(NetworkVariableBase)} instances must be initialized.");
605-
}
606-
600+
var instance = (NetworkVariableBase)sortedFields[i].GetValue(this) ?? throw new Exception($"{GetType().FullName}.{sortedFields[i].Name} cannot be null. All {nameof(NetworkVariableBase)} instances must be initialized.");
607601
instance.Initialize(this);
608602

609603
var instanceNameProperty = fieldType.GetProperty(nameof(NetworkVariableBase.Name));

com.unity.netcode.gameobjects/Runtime/Messaging/DisconnectReasonMessage.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ internal struct DisconnectReasonMessage : INetworkMessage
88

99
public void Serialize(FastBufferWriter writer, int targetVersion)
1010
{
11-
string reasonSent = Reason;
12-
if (reasonSent == null)
13-
{
14-
reasonSent = string.Empty;
15-
}
11+
string reasonSent = Reason ?? string.Empty;
1612

1713
// Since we don't send a ConnectionApprovedMessage, the version for this message is encded with the message
1814
// itself. However, note that we HAVE received a ConnectionRequestMessage, so we DO have a valid targetVersion

com.unity.netcode.gameobjects/Runtime/Serialization/NetworkObjectReference.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,7 @@ public NetworkObjectReference(GameObject gameObject)
5454
throw new ArgumentNullException(nameof(gameObject));
5555
}
5656

57-
var networkObject = gameObject.GetComponent<NetworkObject>();
58-
59-
if (networkObject == null)
60-
{
61-
throw new ArgumentException($"Cannot create {nameof(NetworkObjectReference)} from {nameof(GameObject)} without a {nameof(NetworkObject)} component.");
62-
}
63-
57+
var networkObject = gameObject.GetComponent<NetworkObject>() ?? throw new ArgumentException($"Cannot create {nameof(NetworkObjectReference)} from {nameof(GameObject)} without a {nameof(NetworkObject)} component.");
6458
if (networkObject.IsSpawned == false)
6559
{
6660
throw new ArgumentException($"{nameof(NetworkObjectReference)} can only be created from spawned {nameof(NetworkObject)}s.");

testproject/Assets/Tests/Runtime/MultiprocessRuntime/TestCoordinator.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -453,18 +453,8 @@ public void InvokeFromMethodNameClientRpc(string methodInfoString, byte[] args,
453453
var split = methodInfoString.Split(k_MethodFullNameSplitChar);
454454
var (classToExecute, staticMethodToExecute) = (split[0], split[1]);
455455

456-
var foundType = Type.GetType(classToExecute);
457-
if (foundType == null)
458-
{
459-
throw new Exception($"couldn't find {classToExecute}");
460-
}
461-
462-
var foundMethod = foundType.GetMethod(staticMethodToExecute, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
463-
if (foundMethod == null)
464-
{
465-
throw new MissingMethodException($"couldn't find method {staticMethodToExecute}");
466-
}
467-
456+
var foundType = Type.GetType(classToExecute) ?? throw new Exception($"couldn't find {classToExecute}");
457+
var foundMethod = foundType.GetMethod(staticMethodToExecute, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static) ?? throw new MissingMethodException($"couldn't find method {staticMethodToExecute}");
468458
foundMethod.Invoke(null, args != null ? new object[] { args } : null);
469459
}
470460
catch (Exception e)

0 commit comments

Comments
 (0)