Photon RPC methodcall

So I have code for instantiating an object with an RPC call with Photon but it doesn’t work. I get the debugger message of “RPCs can only be sent in rooms. Call of “Fire” gets executed locally only, if at all.” The code is:

button.onClick.AddListener(Task);

[PunRPC]

void Task()
{
    PhotonNetwork.CreateRoom("Room1");
    PhotonNetwork.JoinRoom("Room1");
    PhotonNetwork.LoadLevel("Map1");
    PhotonNetwork.AutomaticallySyncScene = true;

    if (PhotonNetwork.JoinRoom("Room1") == true)
    { 
        PhotonView photonview = GameObject.Find("FPSController").GetComponent<PhotonView>();
        photonview.RPC("Fire", RpcTarget.AllBuffered);
    }
}

void Fire()
{
    PhotonNetwork.Instantiate("FPSController", new Vector3(316, 0, 124.4414f), transform.rotation);
}

Any help would be fine

Hey there,

there are multiple thing that i’d suggest to change in your code.
The most crutial part here is that when you create a room and join a room, you should ALWAYS wait until the action was actually completed by photon. Use the Callbacks offered by Photon for this.
The error you get indicates that you try to call Fire when you have not yet joined a room. You can only join rooms if the Callback OnJoinedRoom was triggered.
So whenever you try to call upon an RPC there should always be a check on PhotonNetwork.InRoom (might not be exactly the right syntax, depending if you use photon 1 or 2) before it’s call. Only if this is true you can call rpcs.

Also about your fire function: Simply don’t do this. PhotonNetwork.Instantiate will work on your instance the same as the usual normal Instantiate(…) but it will also automatically create the same object at the same position at all other players that are currently in the room or join later on.
So remove the Fire rpc completly and only instatiate the player object at your local game istance when you receive the OnJoinedRoom Callback.

Check out this list HERE for all available callbacks that photon offers. If something was not clear or you have further questions feel free to ask.