I’ve been fighting with Photon Unity Networking for a few days now with trying to instantiate my rocket projectile properly. I’ve been told by Tobias on the Exit Games forums that using PhotonNetwork.Instantiate is completely unnecessary (good thing, too… because it didn’t do anything) and that it’s best to just use an RPC call. I’ve tried that with this code:
void fireRocket(Vector3 bottleRocketStart, Quaternion bottleRocketRot)
{
GetComponent<PhotonView>().RPC("fireRocket_RPC", PhotonTargets.MasterClient, transform.position, Quaternion.identity, nm.teamID);
GetComponent<GetItem>().currentItem = "";
}
[RPC]
void fireRocket_RPC(Vector3 bottleRocketStart, Quaternion bottleRocketRot, int teamID)
{
bottleRocket = (GameObject)Instantiate(Resources.Load("BottleRocketProjectile"), mouseWeaponOrigin.transform.position, Camera.main.transform.rotation);
bottleRocket.GetComponent<RocketInfo>().shotByPlayer = teamID;
}
This successfully spawns the rocket. However, I run into issues when trying to destroy the rocket in this script, which is attached to the rocket prefab.
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name != "BottleRocketProjectile(Clone)" && col.gameObject.name != PhotonNetwork.playerName) {
GetComponent<PhotonView>().RPC("spawnExplosion", PhotonTargets.MasterClient);
GetComponent<PhotonView>().RPC("DestroyOnNetwork", PhotonTargets.MasterClient);
}
}
[RPC]
void spawnExplosion()
{
GameObject bottleRocketSplat = (GameObject)Instantiate(Resources.Load("BottleRocketSplat"), transform.position, transform.rotation);
}
[RPC]
public void DestroyOnNetwork()
{
Destroy(gameObject);
}
The explosion is spawned, but the rocket is not destroyed. I get errors here complaining about viewIDs… which makes sense, since I’m not PhotonNetwork.Instantiating them. That’s where I’m losing track of what I’m supposed to be doing. If I’m not supposed to instantiate them on the network, how do I properly call the RPCs to handle the destruction of the rocket and instantiation of the explosion?
These are the errors I get on collision with something
Illegal view ID:0 method: spawnExplosion GO:BottleRocketProjectile(Clone)
UnityEngine.Debug:LogError(Object)
NetworkingPeer:RPC(PhotonView, String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2915)
PhotonNetwork:RPC(PhotonView, String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2496)
PhotonView:RpcSecure(String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:589)
PhotonView:RPC(String, PhotonTargets, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:557)
RocketInfo:OnCollisionEnter(Collision) (at Assets/RocketInfo.cs:24)
Illegal view ID:0 method: DestroyOnNetwork GO:BottleRocketProjectile(Clone)
UnityEngine.Debug:LogError(Object)
NetworkingPeer:RPC(PhotonView, String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2915)
PhotonNetwork:RPC(PhotonView, String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2496)
PhotonView:RpcSecure(String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:589)
PhotonView:RPC(String, PhotonTargets, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:557)
RocketInfo:OnCollisionEnter(Collision) (at Assets/RocketInfo.cs:25)
PhotonView with ID 0 has no method "DestroyOnNetwork" marked with the [RPC](C#) or @RPC(JS) property! Args:
UnityEngine.Debug:LogError(Object)
NetworkingPeer:ExecuteRPC(Hashtable, PhotonPlayer) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2190)
NetworkingPeer:RPC(PhotonView, String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2979)
PhotonNetwork:RPC(PhotonView, String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2496)
PhotonView:RpcSecure(String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:589)
PhotonView:RPC(String, PhotonTargets, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:557)
RocketInfo:OnCollisionEnter(Collision) (at Assets/RocketInfo.cs:25)