Help with Pun RPC to set parent

Options
Hey, i'm trying to make a shooters 2D game
i'm trying to do that when you spawn a gun will respawn as child of the player (because if I don't do it it will be very laggy for the other player).

I've succeeded to do it with my play but as i see when i'm trying to send the RPC to the other player i get error that the gun is null.

the guns are spawned but only the player that has the view can see the gun attached to him but it's not attached to the other player.

Here is the code:

private void Awake () {
if (photonView.isMine)
{
gun = PhotonNetwork.Instantiate(Resources.Load("M16A3").name, new Vector3(transform.position.x, transform.position.y, transform.position.z), Quaternion.identity, 0);

gun.transform.parent = transform;
photonView.RPC("SetParent", PhotonTargets.Others);
}

}

[PunRPC]
public void SetParent()
{
gun.transform.parent = transform;
}



Here is my error:
NullReferenceException: Object reference not set to an instance of an object PlayerHoldingWeapon.SetParent () (at Assets/PcompleteFiles/scripts/Weapons/PlayerHoldingWeapon.cs:52) System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222) Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation. System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232) System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115) NetworkingPeer.ExecuteRpc (ExitGames.Client.Photon.Hashtable rpcData, Int32 senderID) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2999) NetworkingPeer.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2592) ExitGames.Client.Photon.PeerBase.DeserializeMessageAndCallback (System.Byte[] inBuff) ExitGames.Client.Photon.EnetPeer.DispatchIncomingCommands () ExitGames.Client.Photon.PhotonPeer.DispatchIncomingCommands () PhotonHandler.Update () (at Assets/Photon Unity

Comments

  • Hi @RotemPhoton,

    the problem is, that the other clients don't have the reference to the gun object, only the Owner of the object has it (it's part of the code inside the photonView.isMine condition). To avoid this, you can for example send the ViewId of the gun's PhotonView component with the RPC. Inside the RPC function, the clients can find the object with this certain ViewId and set its parent object. Another way would be, to use InstantiationData when instantiating the gun object. Here you can add the ViewId of the parent object (the player). This way the object can parent itself to the correct player object in its Awake or Start function.