You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
publicclassExample:NetworkBehaviour{privatevoidStart(){TransformnewParent=null;// -------------> Technically means set the parent of the object to root.
NetworkObject.Spawn();
NetworkObject.TrySetParent(newParent);// --------------> Throws a null reference exception.}}
publicboolTrySetParent(Transformparent,boolworldPositionStays=true){varnetworkObject= parent.GetComponent<NetworkObject>();// ----------> Why does this not check for null?// If the parent doesn't have a NetworkObjet then return false, otherwise continue trying to parentreturnnetworkObject==null?false: TrySetParent(networkObject, worldPositionStays);}
There is a workaround, but that's not the point. The workaround is:
publicclassExample:NetworkBehaviour{privatevoidStart(){TransformnewParent=null;
NetworkObject.Spawn();if(parent!=null)
NetworkObject.TrySetParent(newParent);else
NetworkObject.TrySetParent((NetworkObject)null);// --------> The overload that accepts a NetworkObject perfectly accepts a null value.}}
The reasons I consider this a bug and not a feature (of overloading methods) are two:
UnityEngine's transform.SetParent() perfectly accepts null as value.
The naming convention of putting Try in front of methods (TrySomething) means it's not supposed to throw you an exception.
The text was updated successfully, but these errors were encountered:
Here's an example code:
Here's the offending overload:
com.unity.netcode.gameobjects/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs
Line 734 in 54666c3
There is a workaround, but that's not the point. The workaround is:
The reasons I consider this a bug and not a feature (of overloading methods) are two:
UnityEngine
'stransform.SetParent()
perfectly acceptsnull
as value.Try
in front of methods (TrySomething
) means it's not supposed to throw you an exception.The text was updated successfully, but these errors were encountered: