The Photon Forum
is Closed Permanently.

After many dedicated years of service, we have made the decision to retire our Forum and switch to read-only: we´ve saved the best to last! Your search result can be found below. Plus, we offer support via these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

PUNRPC in coroutine

BillyTheKid
2018-12-11 02:52:53

I PhotonNetwork.Instanciate on Start() a GameObject with a photon view and a script. this script has an PUNRPC method inside that changes its color. This method is called from inside the same script, only from the master client, every 5 seconds inside a coroutine like this:

if(PhotonNetwork.IsMasterClient)
GetComponent().RPC("ChangeColor", RpcTarget.AllBuffered);

The problem is that the rpc method executes only for the master client' s Gameobject, altough the same script is inside the objects that the rest of the clients PhotonNetwork.Instansiated. I tried calling the method in Update() when I rpess a button and it is called for every object as it should. Is there a problem with coroutines or something?

Comments

BillyTheKid
2018-12-11 03:04:48

Also the coroutine that sends the RPC is executed on Start() only if (GetComponent().IsMine). I noticed that if I remove that restriction and let all the objects run the coroutine, the RPC method is called like it sould.

[Deleted User]
2018-12-11 16:42:15

Hi @BillyTheKid,

using an RPC from inside a Coroutine works. Please check, if the [PunRPC] marked function gets called on all clients. You can do this for example by adding a Debug.Log call to that function and check if it gets printed to the console.

A few hints: you are currently using RpcTarget.AllBuffered. Instead of this you should use RpcTarget.All because a client, who joins the game later, is not interested in the color an object had earlier. And since you are changing this color very often, you don't need to buffer it. Another thing: if you want to have the color synchronized, you should send it with the RPC to make all clients apply the same color to the same object.

You can take a look at the code snippet below to see how you can implement the Coroutine and the [PunRPC] marked function:

[PunRPC]  
public void ChangeColor(Vector3 color)  
{  
    renderer.material.color = new Color(color.x, color.y, color.z);  
}

private IEnumerator ChangeColorCoroutine()  
{  
    WaitForSeconds wfs = new WaitForSeconds(3.0f);

    while (true)  
    {  
        Vector3 color = new Vector3(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f));

        pView.RPC("ChangeColor", RpcTarget.All, color);

        yield return wfs;  
    }  
}

Please feel free to ask if you have further questions.

BillyTheKid
2018-12-11 17:44:20

I want the ChangeColor function to run simultaneously to all clients. Thats why I want the master client to handle the Timer(coroutine), and tell every client to execute the same RPC. But if I add this restriction

if (PhotonNetwork.IsMasterClient && photonView.IsMine)
pView.RPC("ChangeColor", RpcTarget.All, color);

this script runs only for the GameObject that the master Instansiated thru network, although the rpc method is in every client. I am suspecting that the rpc is targeting only to the Masters PhotoView (I checked what you told me)

[Deleted User]
2018-12-13 09:05:18

this script runs only for the GameObject that the master Instansiated thru network, although the rpc method is in every client. I am suspecting that the rpc is targeting only to the Masters PhotoView (I checked what you told me)

RPCs are only executed on the same PhotonViews they were called from. So if you call an RPC on object A, it will be executed only on object A. It won't get called on object B or other objects. You would have to use the RPC function on each object if you want to have this behaviour. This can be done by the MasterClient, too.

Back to top