Quantcast
Channel: Photon Unity Networking (PUN) — Photon Engine
Viewing all articles
Browse latest Browse all 8947

Variable doesn't sync using RPC

$
0
0
I'm building a multiplayer game that needs to know how many players are in the game to assign the player number when someone new joins, spawn him in a certain place, etc.

This is how the Inspector of the gameManager looks like:



Those are the PunRPC methods that I call to get and set the variable of players on the NetworkPlayerManager class:

public int numPlayer;

[PunRPC]
public void GetPlayers()
{
GetComponent().setNumOfPlayers(numPlayer);
}

[PunRPC]
public void SetPlayers(int num)
{
numPlayer = num;
}



And her's how I call them when a new player is Instantiated on the GameManager class:

GetComponent().RPC("GetPlayers", PhotonTargets.AllViaServer);

m_Tanks.m_Instance =
PhotonNetwork.Instantiate("Tank", m_Spawns[numOfPlayers].position, m_Spawns[numOfPlayers].rotation,0) as GameObject;

numOfPlayers += 1;

m_Tanks.m_PlayerNumber = numOfPlayers;

GetComponent().RPC("SetPlayers", PhotonTargets.AllViaServer, numOfPlayers);


Using this method, once one player enters the game, the value stays in 1 and it doesn't change. It's like the variable doesn't sync, and for every new Instance, it's set to be the player 1.

I've tried to use OnPhotonSerializeView too to make the sync, but it didn't do nothing using:

if (stream.isWriting)
{
stream.SendNext(numPlayer);
}
else
{
numPlayer = (int)stream.ReceiveNext();
}


I think that I have an error of concept on how to synchronise variables. I've searched and looked, but I haven't find what I'm doing wrong.

Viewing all articles
Browse latest Browse all 8947

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>