Photon RPC Only Working Halfway
Hello everyone, thank you for taking the time to give me a hand here. I am using PUN for my server, and I am trying to have colored names and UI sprites that show where your teammates are in the world. When I instantiate a player, I want the color of their names and the actual text of the names to change. (I am using 3d text for the names.) I also want to change the color of the triangle that marks each player. Each player gameobject already has a canvas and a white triangle attached to it. It also has the 3d text as a child of the main object. Here are the two main functions for spawning the player:
public void SpawnPlayer(Vector3 SpawnPos)
{
MyPlayer = PhotonNetwork.Instantiate ("Character", SpawnPos, Quaternion.identity, 0);
Debug.Log ("Instantiated player at " + SpawnPos);
AnimComponent = MyPlayer.GetComponentInChildren<Animator> ();
Debug.Log (AnimComponent.name);
MyPlayer.transform.Find("CharacterModel").GetComponent<Animations> ().enabled = true;
MyPlayer.GetComponent<NetworkCharacterScript> ().Anim = AnimComponent;
TeamScript = MyPlayer.GetComponent<Team> ();
PlayerUIScript = MyPlayer.GetComponent<PlayerID> ();
PlayerUIScript.TeamScript = TeamScript;
MyPlayer.GetComponent<RespawnUI> ().enabled = true;
MyPlayer.GetComponent<Respawn> ().enabled = true;
MyPlayer.GetComponent<Respawn> ().TeamScript = TeamScript;
MyPlayer.GetComponent <PlayerShooting>().enabled = true;
MyPlayer.GetComponent <MouseLook> ().enabled = true;
MyPlayer.GetComponent<MovementScript>().enabled = true;
PlayerUIScript.TeamScript = MyPlayer.GetComponent<Team> ();
PlayTri = MyPlayer.transform.FindChild ("PlayerTriangleCanvas/PlayerTriangle").gameObject;
PlayTri.tag = "Triangle";
NameText = MyPlayer.transform.FindChild ("CharacterName").gameObject;
NameText.tag = "NameText";
GetComponent<PhotonView> ().RPC ("ChangeTrisAndName", PhotonTargets.All, SavedVarsScript.CurrentPlayerName);
PhotonNetwork.isMessageQueueRunning = true;
}
This is the method that actually synchronizes the colors and text:
[PunRPC]
public void ChangeTrisAndName(string PlayerName)
{
ProcessLog ("Function executed for " + PlayerName);
TeamScript.TeamID = MyTeamID;
GameObject[] Triangles = GameObject.FindGameObjectsWithTag ("Triangle");
foreach (GameObject Tri in Triangles)
{
if (Tri.transform.parent.transform.parent.GetComponent<Team> ().TeamID == 1)
{
Tri.GetComponent<Image> ().color = Color.blue;
Tri.tag = "TrianglesBlue";
}
if (Tri.transform.parent.transform.parent.GetComponent<Team> ().TeamID == 2)
{
Tri.GetComponent<Image> ().color = Color.red;
Tri.tag = "TrianglesRed";
}
}
GameObject[] Names = GameObject.FindGameObjectsWithTag ("NameText");
foreach (GameObject Name in Names)
{
if (Name.transform.parent.GetComponent<Team> ().TeamID == 1)
{
Name.GetComponent<TextMesh> ().color = Color.blue;
Name.GetComponent<TextMesh> ().text = SavedVarsScript.CurrentPlayerName;
}
if (Name.transform.parent.GetComponent<Team> ().TeamID == 2)
{
Name.GetComponent<TextMesh> ().color = Color.red;
Name.GetComponent<TextMesh> ().text = SavedVarsScript.CurrentPlayerName;
}
}
}
Sorry for the long scripts, but most of this stuff is actually relevant to the problem. What happens is when I join a game, both players' screens will display the same message that says "Executed function for " and will both display the same player name- the name of the last person to join and the person who actually executed the method. The colors and text for your player is changed, but the person on the other screen's name is still white and displayed as "PlayerName." I can't tell if this is a problem with local variables, the RPC, or something else. I appreciate your help. :)
Edit: The "ProcessLog" method just displays some text on the screen. It is my own debug method that is visible in the final build.
Answer by lucas216erickson · Nov 23, 2016 at 04:57 AM
Bump... I'm really stumped on this one. I've changed this thing around so many times, this must be the 7th or 8th try...
Your answer
Follow this Question
Related Questions
Instantiate with Photon 0 Answers
[PHOTON] Transform not synching correctly 0 Answers