Variables not updated in PUN RPC function
I'm still somewhat new to Unity and have been wrestling with this problem for a several days now. I am trying to send an array of values from the master client to any other client that joins using RPC functions. Basically, if the player is the Master client, createMaze is called and the value of rem_walls is set. I can see in the viewport when running the game that this is true. All other clients call the function createMaze2() which uses an RPC call to the Master to request the value for rem_walls, and the Master client sends this value back. However, for whatever reason, the value of rem_walls that is being sent is not the updated value of rem_walls. Hopefully the code below will make things more clear. Basically, in send_info() rem_walls is always empty, despite the fact that the variable has been updated and reflects this update when I check it in the viewport. Any help would be greatly appreciated.
public void createMaze2(){
//List<Vector3Int> to_rem;
initMaze ();
//PhotonView photonView = PhotonView.Get (this);
if (!PhotonNetwork.isMasterClient) {
this.photonView.RPC ("send_info", PhotonTargets.MasterClient, PhotonNetwork.player);
}
}
[PunRPC]
void send_info(PhotonPlayer plyr){
if (PhotonNetwork.isMasterClient) {
Debug.Log ("sending info");
//PhotonView photonView = PhotonView.Get (this);
Debug.Log (this.rem_walls);
Debug.Log (this.rem_walls.Count);
int[] ai = this.rem_walls.ToArray ();
this.photonView.RPC ("get_info", plyr, ai);
} else {
Debug.Log ("something is wrong");
}
}
[PunRPC]
void get_info(int [] x){
Debug.Log ("getting info");
Debug.Log (x);
rem_walls =new List<int>( x);
Debug.Log (x.Length);
for (int i = 0; i < x.Length; i=i+3) {
Debug.Log (i);
Destroy (Walls [x[i+2], x[i], x[i+1]].gameObject);
}
}
public void createMaze () {
rem_walls.Add(1);
}
Hi,
can you confirm that rem_walls.Count is already up to date when send_info
is being called? Since you already have a Debug.Log(...)
call around it I guess you have already taken a look at that. Can you also confirm that int[] ai
is properly set? Can please also describe what happens in init$$anonymous$$aze()
function? Otherwise I currently don't see, why this is not working.
Thanks for the response. rem_walls.Count is up to date when send_info is called because I looked at the variable in the editor as the game is run and it has the correct values. The Debug.Log(rem_walls.Count) however prints 0. As far as I can tell, ai is properly set. Though it shouldn't matter as I also tried doing this with just a single int rather than an array and it had the same issues. init$$anonymous$$aze() basically just instantiates a bunch of gameobjects in the scene for the map. I can't imagine it has anything to do with the RPC call issues.
Can you maybe share this project stripped-down to its problems so that I can reproduce it because currently I am out of ideas what the problem might be.
Answer by Hsni · Dec 03, 2018 at 08:32 AM
I have same problem. Someone please help.
public static int lvl = 0;
bool isLocked = false;
int temp = 0;
// Use this for initialization
void OnEnable () {
if (!isLocked)
{
temp = Random.Range(0, 10);
UnityEngine.Debug.Log(isLocked);
GetComponent<PhotonView>().RPC("UpdateVariables", PhotonTargets.All);
}
}
[PunRPC]
void UpdateVariables()
{
lvl = temp;
isLocked = true;
}
// Update is called once per frame
void Update () {
UnityEngine.Debug.Log(isLocked);
}
Answer by Moongale · Jan 24, 2019 at 12:18 PM
Hi dudes,
I have faced the same problem and realized that variables inside the [PunRPC] functions do not change, i solved my problem via using PhotonNetwork.RaiseEvent,
Good lucks
Your answer
Follow this Question
Related Questions
Help with photon being used to make a mulitplayer fps 0 Answers
Photon RPC 2 Answers
PhotonUnity player move object problem ! 1 Answer
Photon RPC Only Working Halfway 1 Answer