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.

How to RPC a Singleton Function?

ReinhardtWeyers
2022-10-23 08:49:13

Hi guys,

I'm trying to RPC a function in a separate script, attached to a non-networked mesh.

TL;DR - Have a Frame Mesh that downloads an image and applies it as a texture - Want to update this for everyone in the room when a player changes it.

What would be the correct way of executing a RPC call for a function on a different script?

Big thanks!

Comments

Tobias
2022-10-24 15:34:27

This sounds like a case for Custom Properties (this could tell everyone which texture to load).

You can't call RPCs on non-networked objects. In doubt, have a reference to whatever object you want to affect and have the RPC method call that other object's method. That's plain C#...

ReinhardtWeyers
2022-10-25 00:21:22

Thank you for getting back to me Tobias,

I've had a go at Properties but that's way over my head. I'm still learning, needless to say.

I'm actually having a really hard time with RPCs don't really know what I'm doing wrong. I'm simply trying to update a string at this point, but it never syncs across the entire room, each player updates their own string.

Here is the code:

public TextMeshPro NonNetworked;

public string num;

private void Update()

{

this.photonView.RPC("randomNum", RpcTarget.All);

}

[PunRPC]

public void randomNum ()

{

if (Input.GetKeyDown(KeyCode.Alpha8))

{

num = "8";

NonNetworked.text = num;

}

if (Input.GetKeyDown(KeyCode.Alpha9))

{

NonNetworked.text = num;

num = "9";

}

if (Input.GetKeyDown(KeyCode.Alpha0))

{

num = "10";

NonNetworked.text = num;

}

THE SCRIPT IS ATTACHED TO AN OBJECT WITH A PHOTONVIEW.

Tobias
2022-10-25 15:22:04

Your Update() calls another method via network. Every frame.

There is no parameter when you call the RPC, so the value you would set, is not networked.

This will explode when you have multiple players in the network.

As you said you are a beginner .. please begin at the beginning. You don't know how to use Properties? Learn it. It is the right tool. Yes, you could technically use RPCs but you wouldn't use a hammer for everything, just because you only understand hammering...

ReinhardtWeyers
2022-10-25 19:27:08

Thank you for the guidance and time Tobias,

I took the time to learn more about properties, Observables and other concepts and my test project now works + I've gained experience to improve on all my other mechanics!

Thanks again! 😁

Back to top