- Home /
Photonview.RPC Error
In the last days I was trying to make a first person game where you can drive vehicles. When i was trying to syncronize the helicopter movements i found that photonView.RPC was the best method. But, when i started to make my experiments i always got a lots of errors. The one that i found as main is this:
Exception: cannot serialize(): UnityEngine.Transform ExitGames.Client.Photon.Protocol.Serialize (System.IO.MemoryStream dout, System.Object serObject, Boolean setType) ExitGames.Client.Photon.Protocol.SerializeObjectArray (System.IO.MemoryStream dout, System.Object[] objects, Boolean setType) ExitGames.Client.Photon.Protocol.Serialize (System.IO.MemoryStream dout, System.Object serObject, Boolean setType) ExitGames.Client.Photon.Protocol.SerializeHashTable (System.IO.MemoryStream dout, ExitGames.Client.Photon.Hashtable serObject, Boolean setType) ExitGames.Client.Photon.Protocol.Serialize (System.IO.MemoryStream dout, System.Object serObject, Boolean setType) ExitGames.Client.Photon.Protocol.SerializeParameterTable (System.IO.MemoryStream memStream, System.Collections.Generic.Dictionary`2 parameters) ExitGames.Client.Photon.Protocol.SerializeOperationRequest (System.IO.MemoryStream memStream, Byte operationCode, System.Collections.Generic.Dictionary`2 parameters, Boolean setType) ExitGames.Client.Photon.EnetPeer.SerializeOperationToMessage (Byte opc, System.Collections.Generic.Dictionary`2 parameters, EgMessageType messageType, Boolean encrypt) ExitGames.Client.Photon.EnetPeer.EnqueueOperation (System.Collections.Generic.Dictionary`2 parameters, Byte opCode, Boolean sendReliable, Byte channelId, Boolean encrypt, EgMessageType messageType) ExitGames.Client.Photon.PeerBase.EnqueueOperation (System.Collections.Generic.Dictionary`2 parameters, Byte opCode, Boolean sendReliable, Byte channelId, Boolean encrypted) ExitGames.Client.Photon.PhotonPeer.OpCustom (Byte customOpCode, System.Collections.Generic.Dictionary`2 customOpParameters, Boolean sendReliable, Byte channelId, Boolean encrypt) LoadbalancingPeer.OpRaiseEvent (Byte eventCode, System.Object customEventContent, Boolean sendReliable, .RaiseEventOptions raiseEventOptions) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/LoadbalancingPeer.cs:496) NetworkingPeer.OpRaiseEvent (Byte eventCode, System.Object customEventContent, Boolean sendReliable, .RaiseEventOptions raiseEventOptions) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:925) NetworkingPeer.RPC (.PhotonView view, System.String methodName, PhotonTargets target, System.Object[] parameters) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2797) PhotonNetwork.RPC (.PhotonView view, System.String methodName, PhotonTargets target, System.Object[] parameters) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2409) PhotonView.RPC (System.String methodName, PhotonTargets target, System.Object[] parameters) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:257) Move.OnCollisionEnter (UnityEngine.Collision collider) (at Assets/Scripts/Player/Move.cs:135)
And there are other similar erros in other part of code where i try to call RPCs. This is my main script:
using UnityEngine;
using System.Collections;
public class Move : Photon.MonoBehaviour {
float VerSpeed;
float HorSpeed;
float VerSpeedHel;
float HorSpeedHel;
Vector3 realPosition = Vector3.zero;
Quaternion realRotation = Quaternion.identity;
public bool driving = false;
Rigidbody Heli;
Fall fallscript;
PhotonView Heliview;
GameObject Seat;
Vector3 fwdrot = new Vector3(0,0, -10);
Vector3 backrot = new Vector3(0,0, 10);
// Use this for initialization
void Start () {
if (photonView.isMine) {
PhotonNetwork.player.name = Random.Range(0,9999).ToString();
Debug.Log("Player name: " +PhotonNetwork.player);
}
else {
Destroy(rigidbody);
}
}
// Update is called once per frame
void Update () {
if(photonView.isMine) {
if (!driving) {
VerSpeed = Input.GetAxis("Vertical") * Time.deltaTime * 10;
HorSpeed = Input.GetAxis("Horizontal") * Time.deltaTime * 10;
transform.Translate(HorSpeed,0,VerSpeed);
}
else {
if (Input.GetKey(KeyCode.W)) {
photonView.RPC("MoveFwd", PhotonTargets.MasterClient);
}
if (Input.GetKey(KeyCode.S)) {
photonView.RPC("MoveBack", PhotonTargets.MasterClient);
}
if (!Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S)) {
photonView.RPC("Stop", PhotonTargets.MasterClient);
}
if (Input.GetKey(KeyCode.Q)) {
photonView.RPC("rot", PhotonTargets.MasterClient, new Vector3(0,-100,0));
}
if (Input.GetKey(KeyCode.E)) {
photonView.RPC("rot", PhotonTargets.MasterClient, new Vector3(0,100,0));
}
if (transform.position.y < 75) {
photonView.RPC("Up", PhotonTargets.MasterClient);
if (Input.GetKey(KeyCode.Space)) {
photonView.RPC("Force", PhotonTargets.MasterClient, new Vector3(0,70,0));
}
}
if (Input.GetKey(KeyCode.LeftShift)) {
photonView.RPC("Force", PhotonTargets.MasterClient, new Vector3(0,-70,0));
}
if (Input.GetKey(KeyCode.Escape)) {
transform.localPosition = new Vector3(0.3319414f,0.07f,2f);
gameObject.AddComponent<Rigidbody>();
rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
gameObject.GetComponent<BoxCollider>().enabled = true;
fallscript = gameObject.GetComponent<Fall>();
fallscript.enabled = true;
Seat.tag = "EliSeat";
transform.parent = null;
driving = false;
}
}
}
else {
this.gameObject.transform.position = Vector3.Lerp(transform.position, realPosition, 0.1f);
this.gameObject.transform.rotation = Quaternion.Lerp(transform.rotation, realRotation, 0.1f);
}
}
[RPC]
public void rot (Vector3 force) {
transform.parent.parent.GetComponent<Rigidbody>().AddTorque(force);
}
[RPC]
public void MoveFwd () {
transform.parent.parent.transform.rotation = Quaternion.Slerp(Heli.rotation, Quaternion.Euler(0, Heli.rotation.eulerAngles.y,-15), 3);
}
[RPC]
public void MoveBack () {
transform.parent.parent.transform.rotation = Quaternion.Slerp(Heli.rotation, Quaternion.Euler(0, Heli.rotation.eulerAngles.y,15), 3);
}
[RPC]
public void Up () {
transform.parent.parent.GetComponent<Rigidbody>().AddRelativeForce(0,8.81f,0, ForceMode.Acceleration);
Debug.Log("Quack");
}
[RPC]
public void Force(Vector3 force) {
transform.parent.parent.GetComponent<Rigidbody>().AddRelativeForce(force);
Debug.Log("Puff");
}
[RPC]
public void Stop () {
transform.parent.parent.transform.rotation = Quaternion.Slerp(Heli.rotation, Quaternion.Euler(0, Heli.rotation.eulerAngles.y, 0), 3);
}
[RPC]
public void SetChild (Transform parent) {
transform.parent = parent;
}
void OnCollisionEnter (Collision collider) {
if(collider.gameObject.tag == "EliSeat") {
photonView.RPC("SetChild", PhotonTargets.All, collider.transform);
fallscript = gameObject.GetComponent<Fall>();
fallscript.enabled = false;
Destroy(rigidbody);
gameObject.GetComponent<BoxCollider>().enabled = false;
Heliview = transform.parent.parent.gameObject.GetComponent<PhotonView>();
collider.gameObject.tag = "Busy";
Seat = collider.gameObject;
driving = true;
transform.localPosition = new Vector3(0.3319414f,0.0289368f,-2f);
transform.localEulerAngles = new Vector3(0,180,90);
}
}
public void OnPhotonSerializeView (PhotonStream stream, PhotonMessageInfo info) {
if(stream.isWriting) {
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
else {
realPosition = (Vector3)stream.ReceiveNext();
realRotation = (Quaternion)stream.ReceiveNext();
}
}
}
Sorry if my English is not the best.
I'm not sure what causes your error but you do NOT want to send RPCs every frame that a button is pressed, that is going to cause a lot of lagging in your game. just send and receive that information in the OnPhotonSerializeView method.
Answer by tobiass · Jun 12, 2014 at 09:00 AM
The error says it all: "Exception: cannot serialize(): UnityEngine.Transform"
Transforms can't be sent, because the Engine doesn't allow us to instantiate them. You have to send the individual components pos, rot and or scale instead and apply them to some target transform.
RPCs are called via some PhotonView. This is a component of some GameObject. The other clients will execute the called method only on the corresponding GameObject in their client. Due to that, you don't have to send a parent in most cases.
Your answer

Follow this Question
Related Questions
Photon RPC "DestroyRpc" function not found 1 Answer
Photonview with 4 separate cameras 0 Answers
converting Photon stream to JS 2 Answers