NullReferenceException on client when throwing grenade on server

ChristianWiele

Active member
Hi,

if you throw a grenade on the first floor of the demo scene with the player on the master client, you get an exception on the client:

Code:
NullReferenceException: Object reference not set to an instance of an object
Opsive.Shared.Game.ObjectPoolBase.DestroyInternal (UnityEngine.GameObject instantiatedObject) (at <6b591a2f8f52493cbf5b07f21a33a5d1>:0)
Opsive.Shared.Game.ObjectPoolBase.Destroy (UnityEngine.GameObject instantiatedObject) (at <6b591a2f8f52493cbf5b07f21a33a5d1>:0)
Opsive.UltimateCharacterController.Items.Actions.ThrowableItem.ThrowItem () (at Assets/Opsive/UltimateCharacterController/Scripts/Items/Actions/ThrowableItem.cs:499)
Opsive.UltimateCharacterController.Items.Actions.ThrowableItem.StopItemUse () (at Assets/Opsive/UltimateCharacterController/Scripts/Items/Actions/ThrowableItem.cs:622)
Opsive.UltimateCharacterController.Character.Abilities.Items.Use.AbilityStopped (System.Boolean force) (at Assets/Opsive/UltimateCharacterController/Scripts/Character/Abilities/Items/Use.cs:866)
Opsive.UltimateCharacterController.Character.Abilities.Ability.StopAbility (System.Boolean force, System.Boolean fromController) (at Assets/Opsive/UltimateCharacterController/Scripts/Character/Abilities/Ability.cs:692)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion.TryStopAbility (Opsive.UltimateCharacterController.Character.Abilities.Ability ability, System.Boolean force) (at Assets/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotion.cs:1440)
Opsive.UltimateCharacterController.AddOns.Multiplayer.PhotonPun.Character.PunCharacter.OnItemAbilityActiveRPC (System.Int32 itemAbilityIndex, System.Boolean active) (at Assets/Opsive/UltimateCharacterController/Add-Ons/Multiplayer/PhotonPUN/Scripts/Character/PunCharacter.cs:232)
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
Photon.Pun.PhotonNetwork.ExecuteRpc (ExitGames.Client.Photon.Hashtable rpcData, Photon.Realtime.Player sender) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:544)
Photon.Pun.PhotonNetwork.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:2203)
Photon.Realtime.LoadBalancingClient.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:3289)
ExitGames.Client.Photon.PeerBase.DeserializeMessageAndCallback (ExitGames.Client.Photon.StreamBuffer stream) (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/PeerBase.cs:891)
ExitGames.Client.Photon.EnetPeer.DispatchIncomingCommands () (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/EnetPeer.cs:558)
ExitGames.Client.Photon.PhotonPeer.DispatchIncomingCommands () (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/PhotonPeer.cs:1837)
Photon.Pun.PhotonHandler.Dispatch () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:223)
Photon.Pun.PhotonHandler.FixedUpdate () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:149)

UCC 2.3.2
Photon PUN 2.29
PUN add-on 1.1.11

Regards, Christian
 
Thanks, this was part of the 2.3.2 rerelease. Go ahead and download it again (I also noticed something in PunObjectPool so am resubmitting that).
 
Ok, I am a little lost. I have two fresh installations on Unity 2020.3.3. One with URP and one with built-in RP. I deleted all Opsive package downloads to be sure I'm installing the correct packages. Downloaded all current packages UCC 2.3.2 and add-on 1.1.11.
Installed the packages into two empty projects.

1.) On the built-in RP version everything works fine.
2.) On the URP-version I still get the exception.

The only difference is URP and the URP integration package. Otherwise there is nothing else installed (besides Photon PUN 2.29).
 
Are you sure that you have the latest on the URP version? URP would not affect this error.
 
Yes, and it is completely strange. I just downloaded everything again to be really sure to have the latest version. I installed the URP version. If I run the demo scene before baking the light, everything works fine. After generating the light (without baked GI) and building the project again, I get the exception.
 
And, well, now I again get the exception also on the non-URP version after installing the version downloaded this morning.
 
There isn't a way you should be getting that same exception. It was caused by a null object and there's now a null object check there. Could it be a different stack trace?
 
This is the exception I am getting on the latest version:

Code:
NullReferenceException: Object reference not set to an instance of an object
Opsive.Shared.Game.ObjectPoolBase.DestroyInternal (UnityEngine.GameObject instantiatedObject) (at <30943c6321fb4a8cbbf1f4853f76af59>:0)
Opsive.Shared.Game.ObjectPoolBase.Destroy (UnityEngine.GameObject instantiatedObject) (at <30943c6321fb4a8cbbf1f4853f76af59>:0)
Opsive.UltimateCharacterController.Items.Actions.ThrowableItem.ThrowItem () (at Assets/Opsive/UltimateCharacterController/Scripts/Items/Actions/ThrowableItem.cs:499)
Opsive.UltimateCharacterController.Items.Actions.ThrowableItem.StopItemUse () (at Assets/Opsive/UltimateCharacterController/Scripts/Items/Actions/ThrowableItem.cs:622)
Opsive.UltimateCharacterController.Character.Abilities.Items.Use.AbilityStopped (System.Boolean force) (at Assets/Opsive/UltimateCharacterController/Scripts/Character/Abilities/Items/Use.cs:866)
Opsive.UltimateCharacterController.Character.Abilities.Ability.StopAbility (System.Boolean force, System.Boolean fromController) (at Assets/Opsive/UltimateCharacterController/Scripts/Character/Abilities/Ability.cs:692)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion.TryStopAbility (Opsive.UltimateCharacterController.Character.Abilities.Ability ability, System.Boolean force) (at Assets/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotion.cs:1440)
Opsive.UltimateCharacterController.AddOns.Multiplayer.PhotonPun.Character.PunCharacter.OnItemAbilityActiveRPC (System.Int32 itemAbilityIndex, System.Boolean active) (at Assets/Opsive/UltimateCharacterController/Add-Ons/Multiplayer/PhotonPUN/Scripts/Character/PunCharacter.cs:232)
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
Photon.Pun.PhotonNetwork.ExecuteRpc (ExitGames.Client.Photon.Hashtable rpcData, Photon.Realtime.Player sender) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:544)
Photon.Pun.PhotonNetwork.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:2203)
Photon.Realtime.LoadBalancingClient.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:3289)
ExitGames.Client.Photon.PeerBase.DeserializeMessageAndCallback (ExitGames.Client.Photon.StreamBuffer stream) (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/PeerBase.cs:891)
ExitGames.Client.Photon.EnetPeer.DispatchIncomingCommands () (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/EnetPeer.cs:558)
ExitGames.Client.Photon.PhotonPeer.DispatchIncomingCommands () (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/PhotonPeer.cs:1837)
Photon.Pun.PhotonHandler.Dispatch () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:223)
Photon.Pun.PhotonHandler.FixedUpdate () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:149)
 
That is the same. What happens if you do a null check before line 499 of ThrowableItem?
 
The issue seems to be the following. The ThrowItem method is called twice. The first call with a valid reference for m_InstantiatedThrownObject is coming through the ThrowItemRPC. This leads to the destruction of the item in line 499. The second call comes from the OnItemAbilityActive RPC. This leads to a ThrowItem call in line 622 because the variable m_Thrown is false. The reason is that on the client line 550 is never reached where m_Thrown is set to true, because after the first call the ThrowItem method is stopped in line 501.
 
Hmm, ok, I'll take a closer look at this and let you know what I find out. It's strange because those RPCs haven't changed in awhile so I'm surprised that it is coming up now.
 
I don't think the RPCs are the issue. The grenade is thrown correctly. What is going wrong is that when the ability is stopped, it tries to throw the grenade again on the client as m_Thrown is set to false (which is false ;-) ).
 
Top