fix: NetworkManager property more robust on NetworkBehaviour (#2257)

fix: more robust checks around NetworkObjects being null
This commit is contained in:
Jeffrey Rainy 2022-10-19 14:54:00 -04:00 коммит произвёл GitHub
Родитель 2c69184e5f
Коммит 30d23bcc7a
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 21 добавлений и 3 удалений

Просмотреть файл

@ -286,7 +286,18 @@ namespace Unity.Netcode
/// Gets the NetworkManager that owns this NetworkBehaviour instance
/// See note around `NetworkObject` for how there is a chicken / egg problem when we are not initialized
/// </summary>
public NetworkManager NetworkManager => NetworkObject.NetworkManager;
public NetworkManager NetworkManager
{
get
{
if (NetworkObject?.NetworkManager != null)
{
return NetworkObject?.NetworkManager;
}
return NetworkManager.Singleton;
}
}
/// <summary>
/// If a NetworkObject is assigned, it will return whether or not this NetworkObject
@ -349,9 +360,16 @@ namespace Unity.Netcode
{
get
{
if (m_NetworkObject == null)
try
{
m_NetworkObject = GetComponentInParent<NetworkObject>();
if (m_NetworkObject == null)
{
m_NetworkObject = GetComponentInParent<NetworkObject>();
}
}
catch (Exception)
{
return null;
}
// ShutdownInProgress check: