Caught exception in OnEvent() for event code 200:

Options

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?

Answers

  • [Deleted User]
    edited November 2021
    Options

    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
    
  • eligolf
    Options

    Thanks Clemanza, I didn't know it was a null reference. That makes it easier to debug :)

  • maxclark
    Options

    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.