How do I change the child inside a Photon Network instantiated gameobject?
This snippet doesn't work as expected. The change appeared to its child can't be seen in other client.
user =PhotonNetwork.Instantiate("User", new Vector3(0, 1, 0), Quaternion.identity, 0);
if (PlayerPrefs.HasKey("username"))
{
user.transform.GetChild(1).GetComponent().text = PlayerPrefs.GetString("username");
}
What's wrong here and how do I correct it?
Also this RPC doesn't work:
void Start()
{
if (photonView.isMine)
{
this.transform.parent = Camera.main.transform;
this.transform.localPosition = new Vector3(0, 0, 0);
if (PlayerPrefs.HasKey("username"))
{
photonView.RPC("updateusername", PhotonTargets.All);
// user.transform.GetChild(1).GetComponent().text = PlayerPrefs.GetString("username");
}
}
}
[PunRPC]
void updateusername(PhotonMessageInfo info)
{
transform.GetChild(1).GetComponent().text = PlayerPrefs.GetString("username");
}
Comments
-
Hi,
The PlayerPreferences are not synchronized over the network, so sending an RPC to update a value based on local PlayerPrefs isn't going to work.
few solutions:
- use PhotonNetwork.playerName before connection or .owner.NickName this will automatically be synchronized over the network
- you need to send the username string with the RPC message
I would encourage you study the demos shipped with PUN, they will show you how to do this, for example the Basic Demo Tutorial is using this and PlayerPrefs to name players properly.
http://doc.photonengine.com/en-us/pun/current/tutorials/pun-basics-tutorial/lobby-ui#player_name
http://doc.photonengine.com/en-us/pun/current/tutorials/pun-basics-tutorial/player-ui-prefab#bind
If you have more questions, let me know.
Bye,
Jean
0 -
So as per what you told the following snippet should work. However it doesn't work either too.jeanfabre said:Hi,
The PlayerPreferences are not synchronized over the network, so sending an RPC to update a value based on local PlayerPrefs isn't going to work.
few solutions:
- use PhotonNetwork.playerName before connection or .owner.NickName this will automatically be synchronized over the network
- you need to send the username string with the RPC message
I would encourage you study the demos shipped with PUN, they will show you how to do this, for example the Basic Demo Tutorial is using this and PlayerPrefs to name players properly.
http://doc.photonengine.com/en-us/pun/current/tutorials/pun-basics-tutorial/lobby-ui#player_name
http://doc.photonengine.com/en-us/pun/current/tutorials/pun-basics-tutorial/player-ui-prefab#bind
If you have more questions, let me know.
Bye,
Jean
void Start()
{
if (photonView.isMine)
{
if (PlayerPrefs.HasKey("username"))
{
string name = PlayerPrefs.GetString("username"); // or string name = PhotonNetwork.playerName (which is "rgb");
photonView.RPC("updateusername", PhotonTargets.AllBufferedViaServer,name);
}
}
}
[PunRPC]
void updateusername(string name,PhotonMessageInfo info)
{
transform.GetChild(1).GetComponent().text =name;
}0 -
Hi,
- don't use all buffered nor via server for rpc, you don't need this
- can you debug log inside your rpc method to verify you receive it.
- do you have your playerpref key existing, if not rpc will not be called, so debug log also when you actually call the rpc
Bye
Jean1 -
Thank you Jean. That really helped a lot. Now last thing. Can you let me know how to send RPC to only a particular PhotonPlayer instead of All.jeanfabre said:Hi,
- don't use all buffered nor via server for rpc, you don't need this
- can you debug log inside your rpc method to verify you receive it.
- do you have your playerpref key existing, if not rpc will not be called, so debug log also when you actually call the rpc
Bye
Jean0 -
Got it. It is PhotonPlayer.Find(int)0
-
rgb said:
Hey Jean,jeanfabre said:Hi,
- don't use all buffered nor via server for rpc, you don't need this
- can you debug log inside your rpc method to verify you receive it.
- do you have your playerpref key existing, if not rpc will not be called, so debug log also when you actually call the rpc
Bye
Jean
Now can you help me in correcting this. I believe that this is Photon error:
public override void OnPhotonPlayerConnected(PhotonPlayer newPlayer)
{
Debug.Log(newPlayer+ " joined");
if (username!=null)
{
photonView.RPC("updateusername", PhotonPlayer.Find(newPlayer.ID), username);
}
}
[PunRPC]
void updateusername(string name, PhotonMessageInfo info)
{
Debug.Log("RPC being called");
user.transform.GetChild(1).GetComponent().text = name;
}
error:
object not set to an instance of an object
this happens because the object is instantiated in the room after RPC is called. How do I rectify this?0 -
I also think that this is Photon error. Once I also messed up with something like this. Hope someone solves this out here.0
-
Hi,
this has now nothing to do with Photon unfortunatly, when sending an RPC you can not make assumptions on what's going to be on the other instances. So if the child is not present you need ro save the username and when the child is instantiated it will get it from this script.
anyway, it's strongly advice not to use RPC for usernames, as Photon has already a built in mechanism for managing the nickname of the user automatically.
Bye,
Jean0