GetComponent returns non-existing component called in RPC

Options
Hi,

I stumbled upon a weird issue and need help resolving it. I am working on a multiplayer game, based on Unity3D and Photon, where at the player object I have two scripts: one containing the game logic and one containing RPC functions:

Player
|- Script A (game logic)
|- Script B (RPC functions)

Whenever a one player interacts with another player, a RPC function is called, which works like a charm, however inside the RPC function I am using gameObject.GetComponent<Script A>(), expecting that this will give me access to the game logic script assigned to the second player. Well, the call succeeds, however the Script A is some imaginary non-initialized instance... it also has completely different ID, than the "real" Script A, which is running the game for the second player.

I have also tried to hard set a reference between the scripts on gameObject initialization, to avoid the GetComponent call, however in that case in the RPC call the reference is null.

Can anyone explain this behavior and how can I overcome it?

Edit: forgot to mention: absolutely the same code works fine, when the player interacts with himself, instead with another player, however the call is still done via RPC.
Ideas?

Comments

  • vadim
    Options
    Hi,

    Getting reference to Script A does not make sense. You need object's PhotonView component to control remote player over network via RPC.
    Also make sure that gameObject reference assigned properly with local instance of remote player.
  • Hey, thanks for the reply.

    "Player", as referenced above, is already a PhotonView and both scripts are assigned to it. With hard reference I meant not from me to the script of the remote player, but between Script A and Script B on the remote side.

    What do you mean to make sure, that the reference is assigned properly?
    "Player" has two scripts and is initialized as network object on joining the room.

    Am I missing something?
  • More info: observed is only the Player itself (Photon View), saw in forums this might have some implication.
    Any ideas?
  • Tobias
    Options
    You should avoid the problem by simply calling the RPC on the object you actually target. All RPCs relate to the GameObject of the PhotonView which you use to call RPC("rpc",..).

    Aside from that, we can not really help with this problem, as it's not really network related. The RPC works but somehow you don't get the references you expect.
    Sorry.
  • Hey, that helped, thanks :)
    Everything is working now.