[Newbie] Reference GameObject over Photon RPC


I'm very new to using Photon, so go easy on me :) Currently, the user can click on a GUI button, which will initiate a cube within the scene. It's initiated via 'PhotonNetwork.Instantiate'.

As this cube initiates, I also want to give it a random colour. Right now, this only occurs locally. So I need to use an RPC right? Only problem I'm having is reference that cube to the RPC:

[code2=javascript]function OnGUI(){

if(GUI.Button(Rect(0,0,100,50), "Building1")){
var b1 = PhotonNetwork.Instantiate(building1.name, transform.position, Quaternion.identity, 0);
b1.renderer.material = currentMaterial;

var id1 = b1.gameObject.GetComponent(PhotonView);

var photonView : PhotonView = this.GetComponent(PhotonView);
photonView.RPC("PlayerColours", PhotonTargets.AllBuffered, currentMaterial, id1);

function PlayerColours(color : Material, object : PhotonView){

print("[RPC] Player Colour: " + object);

I assume I can't reference the transform/gameObject directly, I can only reference the PhotonView, right? If thats the case, how do I now access the gameObject of the PhotonView within the RPC?

Is this the correct way to do this?

PS - Is it me, or is the API photon have provided awful?

I can't seem to find anything I want, such as 'PhotonView', so I can see what I can access via it.


  • Welcome on board!
    We are usually very relaxed in terms of support, so if you ever think we're not going easy on you (or anyone), then it's likely we're making a joke (which got lost in translation or typing) or we are really busy. Never take the Exit Games team too serious!

    You are on a good path there. Using an RPC is one way to do this.
    The trick here is that the PhotonView is a component. It identifies the GameObject on all clients. When you call an RPC on a specific PhotonView, it will be executed on scripts that are on this object only.
    So in the RPC method, you can access the GameObject and any of it's other components without specifically sending that GameObject (or another ID) around.

    Makes sense?

    The API you found online is not explaining PUN, so you won't find any of those classes.
    The Asset Store package contains a *.pdf (and a *.chm) with the reference doc. It's also online here:
    http://doc-api.exitgames.com/en/realtim ... t/pun/doc/

    More docs and a tutorial are here:
    http://doc.exitgames.com/en/pun/current ... /pun-intro
  • Ah brilliant, yes it does thanks!

    Thanks for the links too, really helpful!
Sign In or Register to comment.