@@ -502,23 +502,11 @@ public NetworkManager NetworkManager
502
502
/// </summary>
503
503
public bool HasAuthority { get ; internal set ; }
504
504
505
- internal NetworkClient LocalClient { get ; private set ; }
506
505
507
506
/// <summary>
508
507
/// Gets whether the client is the distributed authority mode session owner.
509
508
/// </summary>
510
- public bool IsSessionOwner
511
- {
512
- get
513
- {
514
- if ( LocalClient == null )
515
- {
516
- return false ;
517
- }
518
-
519
- return LocalClient . IsSessionOwner ;
520
- }
521
- }
509
+ public bool IsSessionOwner { get ; private set ; }
522
510
523
511
/// <summary>
524
512
/// Gets whether the server (local or remote) is a host.
@@ -675,12 +663,41 @@ internal void UpdateNetworkProperties()
675
663
IsHost = networkManager . IsListening && networkManager . IsHost ;
676
664
IsClient = networkManager . IsListening && networkManager . IsClient ;
677
665
IsServer = networkManager . IsListening && networkManager . IsServer ;
678
- LocalClient = networkManager . LocalClient ;
666
+ IsSessionOwner = networkManager . IsListening && networkManager . LocalClient . IsSessionOwner ;
679
667
HasAuthority = networkObject . HasAuthority ;
680
668
ServerIsHost = networkManager . IsListening && networkManager . ServerIsHost ;
681
669
}
682
670
}
683
671
672
+ private void ResetAllFields ( )
673
+ {
674
+ m_NetworkObject = null ;
675
+ m_NetworkManager = null ;
676
+ RpcTarget = null ;
677
+
678
+ // Set identification related properties
679
+ NetworkObjectId = default ;
680
+ IsLocalPlayer = false ;
681
+
682
+ // This is "OK" because GetNetworkBehaviourOrderIndex uses the order of
683
+ // NetworkObject.ChildNetworkBehaviours which is set once when first
684
+ // accessed.
685
+ NetworkBehaviourId = default ;
686
+
687
+ // Set ownership related properties
688
+ IsOwnedByServer = false ;
689
+ IsOwner = false ;
690
+ OwnerClientId = default ;
691
+
692
+ // Set NetworkManager dependent properties
693
+ IsHost = false ;
694
+ IsClient = false ;
695
+ IsServer = false ;
696
+ IsSessionOwner = false ;
697
+ HasAuthority = false ;
698
+ ServerIsHost = false ;
699
+ }
700
+
684
701
/// <summary>
685
702
/// Only for use in distributed authority mode.
686
703
/// Invoked only on the authority instance when a <see cref="NetworkObject"/> is deferring its despawn on non-authoritative instances.
@@ -762,6 +779,7 @@ internal void NetworkPreSpawn(ref NetworkManager networkManager, NetworkObject n
762
779
{
763
780
m_NetworkObject = networkObject ;
764
781
m_NetworkManager = networkManager ;
782
+ RpcTarget = networkManager . RpcTarget ;
765
783
766
784
UpdateNetworkProperties ( ) ;
767
785
@@ -872,6 +890,8 @@ internal void InternalOnNetworkDespawn()
872
890
{
873
891
NetworkVariableFields [ i ] . Deinitialize ( ) ;
874
892
}
893
+
894
+ ResetAllFields ( ) ;
875
895
}
876
896
877
897
/// <summary>
@@ -1553,7 +1573,15 @@ internal virtual void InternalOnDestroy()
1553
1573
/// </summary>
1554
1574
public virtual void OnDestroy ( )
1555
1575
{
1556
- InternalOnDestroy ( ) ;
1576
+ try
1577
+ {
1578
+ InternalOnDestroy ( ) ;
1579
+ }
1580
+ catch ( Exception ex )
1581
+ {
1582
+ Debug . LogException ( ex ) ;
1583
+ }
1584
+
1557
1585
if ( m_NetworkObject != null && m_NetworkObject . IsSpawned && IsSpawned )
1558
1586
{
1559
1587
// If the associated NetworkObject is still spawned then this
@@ -1574,12 +1602,12 @@ public virtual void OnDestroy()
1574
1602
}
1575
1603
1576
1604
1577
- for ( int i = 0 ; i < NetworkVariableFields . Count ; i ++ )
1605
+ foreach ( var networkVar in NetworkVariableFields )
1578
1606
{
1579
- NetworkVariableFields [ i ] . Dispose ( ) ;
1607
+ networkVar . Dispose ( ) ;
1580
1608
}
1581
1609
1582
- m_NetworkObject = null ;
1610
+ ResetAllFields ( ) ;
1583
1611
}
1584
1612
}
1585
1613
}
0 commit comments