Skip to content

Commit 7a49a9a

Browse files
committed
Convert "==" to explicit Unity engine object lifetime check
1 parent 18a1805 commit 7a49a9a

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ public NetworkObject NetworkObject
574574
{
575575
get
576576
{
577-
if (m_NetworkObject != null)
577+
if (m_NetworkObject)
578578
{
579579
return m_NetworkObject;
580580
}
@@ -594,7 +594,7 @@ public NetworkObject NetworkObject
594594
// or NetworkBehaviour.IsSpawned (i.e. to early exit if not spawned) which, in turn, could generate several Warning messages
595595
// per spawned NetworkObject. Checking for ShutdownInProgress prevents these unnecessary LogWarning messages.
596596
// We must check IsSpawned, otherwise a warning will be logged under certain valid conditions (see OnDestroy)
597-
if (IsSpawned && m_NetworkObject == null && (m_NetworkManager == null || !m_NetworkManager.ShutdownInProgress))
597+
if (IsSpawned && !m_NetworkObject && (!m_NetworkManager || !m_NetworkManager.ShutdownInProgress))
598598
{
599599
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
600600
{

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,7 @@ public sealed class NetworkObject : MonoBehaviour
5252
/// Gets the Prefab Hash Id of this object if the object is registerd as a prefab otherwise it returns 0
5353
/// </summary>
5454
[HideInInspector]
55-
public uint PrefabIdHash
56-
{
57-
get
58-
{
59-
return GlobalObjectIdHash;
60-
}
61-
}
55+
public uint PrefabIdHash => GlobalObjectIdHash;
6256

6357
/// <summary>
6458
/// InstantiationData sent during the instantiation process.
@@ -2167,7 +2161,7 @@ internal void SetNetworkParenting(ulong? latestParent, bool worldPositionStays)
21672161
public bool TrySetParent(Transform parent, bool worldPositionStays = true)
21682162
{
21692163
// If we are removing ourself from a parent
2170-
if (parent == null)
2164+
if (!parent)
21712165
{
21722166
return TrySetParent((NetworkObject)null, worldPositionStays);
21732167
}
@@ -2253,7 +2247,7 @@ public bool TrySetParent(NetworkObject parent, bool worldPositionStays = true)
22532247

22542248
internal bool InternalTrySetParent(NetworkObject parent, bool worldPositionStays = true)
22552249
{
2256-
if (parent != null && (IsSpawned ^ parent.IsSpawned) && !NetworkManager.ShutdownInProgress)
2250+
if (!parent && (IsSpawned ^ parent.IsSpawned) && !NetworkManager.ShutdownInProgress)
22572251
{
22582252
if (NetworkManager.LogLevel <= LogLevel.Developer)
22592253
{
@@ -2265,7 +2259,7 @@ internal bool InternalTrySetParent(NetworkObject parent, bool worldPositionStays
22652259

22662260
m_CachedWorldPositionStays = worldPositionStays;
22672261

2268-
if (parent == null)
2262+
if (!parent)
22692263
{
22702264
CurrentParent = null;
22712265
transform.SetParent(null, worldPositionStays);
@@ -2381,7 +2375,7 @@ private void OnTransformParentChanged()
23812375
var message = new ParentSyncMessage
23822376
{
23832377
NetworkObjectId = NetworkObjectId,
2384-
IsLatestParentSet = m_LatestParent != null && m_LatestParent.HasValue,
2378+
IsLatestParentSet = m_LatestParent is not null,
23852379
LatestParent = m_LatestParent,
23862380
RemoveParent = removeParent,
23872381
AuthorityApplied = authorityApplied,
@@ -2458,15 +2452,15 @@ internal bool ApplyNetworkParenting(bool removeParent = false, bool ignoreNotSpa
24582452
// has been set, this will not be entered into again (i.e. the later code will be invoked and
24592453
// users will get notifications when the parent changes).
24602454
var isInScenePlaced = IsSceneObject.HasValue && IsSceneObject.Value;
2461-
if (transform.parent != null && !removeParent && !m_LatestParent.HasValue && isInScenePlaced)
2455+
if (!transform.parent && !removeParent && !m_LatestParent.HasValue && isInScenePlaced)
24622456
{
24632457
var parentNetworkObject = transform.parent.GetComponent<NetworkObject>();
24642458

24652459
// If parentNetworkObject is null then the parent is a GameObject without a NetworkObject component
24662460
// attached. Under this case, we preserve the hierarchy but we don't keep track of the parenting.
24672461
// Note: We only start tracking parenting if the user removes the child from the standard GameObject
24682462
// parent and then re-parents the child under a GameObject with a NetworkObject component attached.
2469-
if (parentNetworkObject == null)
2463+
if (!parentNetworkObject)
24702464
{
24712465
// If we are parented under a GameObject, go ahead and mark the world position stays as false
24722466
// so clients synchronize their transform in local space. (only for in-scene placed NetworkObjects)
@@ -3176,7 +3170,7 @@ internal SceneObject GetMessageSceneObject(ulong targetClientId = NetworkManager
31763170
{
31773171
var obj = new SceneObject
31783172
{
3179-
HasParent = transform.parent != null,
3173+
HasParent = m_CachedParent is not null,
31803174
WorldPositionStays = m_CachedWorldPositionStays,
31813175
NetworkObjectId = NetworkObjectId,
31823176
OwnerClientId = OwnerClientId,

0 commit comments

Comments
 (0)