Caught exception in OnEvent() for event code 200
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
Caught exception in OnEvent() for event code 200:
eligolf
2021-11-05 11:16:46
Hi,
Thank you for an awesome product, love it so far! I am having some weird issues with RPC calls however and I constantly (and sort of randomly?) get the following error message when playing with 3 players:
Caught exception in OnEvent() for event code 200: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
It happens when I call my RPC function as per below which handles when a player finishes the hole in my minigolf game and lets all other player know it happened:
photonView.RPC(nameof(RPC_FinishedHole), RpcTarget.Others);
...
[PunRPC]
private void RPC_FinishedHole(PhotonMessageInfo info)
{
for (int i = 0; i < courseController.playerObjectsInGame.Count; i++)
{
GameObject player = courseController.playerObjectsInGame[i];
if (player.GetComponent<PhotonView>().Owner.UserId == info.Sender.UserId)
{
// Change status of the ball and remove player from list
player.GetComponent<Ball>().isBallInHole = true;
courseController.playerObjectsInGame.Remove(player);
break;
}
}
// Check if it was the last player on the hole
courseController.CheckForHoleEnded();
}
playerObjectsInGame is how many players who are still playing the hole and havent finished. I go through the objects and see if I found the sending person. If I did, I set some parameters and then remove from the list of gameobjects. Lastly I check if the hole ended (if the list of playerObjectsInGame is <= 0.
Why is the error occuring? I have googled a lot but can't find any relevant reasons. Am I doing something wrong?
Comments
[Deleted User]
2021-11-12 16:05:13
Hello,
An exception was thrown in your RPC function. Check for any NullRefException, especially in your following lines :
for (int i = 0; i < courseController.playerObjectsInGame.Count; i++) //-> check if courseController or playerObjectsInGame are null
player.GetComponent<PhotonView>().Owner.UserId == info.Sender.UserId //-> check if you get a photon view from player, check if Owner, info and Sender are null
player.GetComponent<Ball>().isBallInHole = true; //-> check if you get a Ball from player
Thanks Clemanza, I didn't know it was a null reference. That makes it easier to debug :)
Is there any better way of getting a stack trace from Photon rather than ust "somewhere in the excecution of this RPC there's a exception"?
Sometimes, my RPCs have large bodies and pinpointing where the null reference is is a headahce.
If I wrap the code in a try-block, then I get the line number for the source of the exception, but I don't want to have to wrap all my RPCs in try-catch blocks.
Back to top