- Home /
Photon Network Cant Destroy Game Object In All Clients
Header explains itself. I need help please. Any help would be appriciated.
Answer by Laran · Apr 21, 2015 at 12:36 PM
Function that You use to destroy game object must be marked as an RPC before it can be invoked remotely.
For example:
void myFunction() {
//code...
}
should be:
C#
[RPC]
void myFunction() {
//code...
}
JS
@RPC
function myFunction() {
//code...
}
Following code invokes an RPC function:
networkView.RPC ("myFunction", RPCMode.AllBuffered);
If You have to pass variables to the function, just type it after RPC.Mode.AllBuffered and comma. For example:
networkView.RPC ("myFunction", RPCMode.AllBuffered, myVariable);
You can read more about RPC here: http://docs.unity3d.com/Manual/net-RPCDetails.html
Answer by Laran · Apr 21, 2015 at 10:08 AM
C#
//function used to destroy game object
[RPC]
void networkDestroy(GameObject gO) {
Destroy(gO);
}
// calling the RPC
photonView.RPC("networkDestroy", PhotonTargets.All, objectToDestroy);
Here are some basic information about networking using Photon: http://doc.exitgames.com/th/pun/current/getting-started/pun-intro
I used PhotonTargets.All to send this information to all (clients and server), but You can specify who will receive this RPC. You can read more about this here: Photon Documentation
Sadly this does not work as you cant sent GameObjects over RPC's. You would need to send the ViewID then extract the gameobject from that.
Answer by Jeredriq · May 19, 2015 at 05:01 PM
I've got this code:
public void OnTriggerEnter(Collider col){
if(col.gameObject.tag=="Knife")
photonView.RPC("networkDestroy", PhotonTargets.All, transform.parent.gameObject);
}
[RPC]
public void Die(GameObject parent)
{ Destroy (parent);
}
but it seems to doesnt work. It gives me this error
Exception: cannot serialize(): UnityEngine.GameObject ExitGames.Client.Photon.Protocol.Serialize (System.IO.MemoryStream dout, System.Object serObject, Boolean setType)