- Home /
Photon handling spawn object with master client
Hi everyone I have a problem with the spawn of objects and enemies. So I have 2 methods:
[PunRPC]
public void SpawnBronzeChest() {
if(PhotonNetwork.isMasterClient) {
GameObject bsp = GameObject.FindGameObjectWithTag("Bronze Chest Spawn");
Transform[] bronzeSpawnPoints = new Transform[bsp.transform.childCount];
for(int i = 0; i < bsp.transform.childCount; i++) {
bronzeSpawnPoints[i] = bsp.transform.GetChild(i);
Vector3 chestPosition = bronzeSpawnPoints[i].position;
GameObject bronzeChest = PhotonNetwork.InstantiateSceneObject(chestBronze, chestPosition, Quaternion.identity, 0, null);
}
}
}
[PunRPC]
void SpawnEnemies() {
if(PhotonNetwork.isMasterClient) {
GameObject esp = GameObject.FindGameObjectWithTag("EnemySpawn");
Transform[] enemySpawnPoint = new Transform[esp.transform.childCount];
for(int i = 0; i < esp.transform.childCount; i++) {
enemySpawnPoint[i] = esp.transform.GetChild(i);
Vector3 enemyPosition = enemySpawnPoint[i].position;
GameObject enemy = PhotonNetwork.InstantiateSceneObject(enemyPrefab, enemyPosition, Quaternion.identity, 0, null);
}
}
}
I call this RPC in:
void OnJoinedRoom() {
SpawnPlayer();
photonView.RPC("SpawnBronzeChest", PhotonTargets.MasterClient);
photonView.RPC("SpawnEnemies", PhotonTargets.MasterClient);
}
But when I play for each client the scripts spawn the chests and the enemies, the control (PhotonNetwork.isMasterClient) is always true...
Answer by ChristianSimon · Jun 12, 2017 at 01:10 PM
Hi,
each clients calls OnJoinedRoom() when he joined the room and furthermore each time a client enters the room, he will send a RPC to the MasterClient in order to instantiate chests and enemies which the MasterClient does afterwards.
Maybe you want to move the PhotonNetwork.isMasterClient condition at the beginning of the OnJoinedRoom callback and spawn everything you need inside that condition.
void OnJoinedRoom()
{
if (PhotonNetwork.isMasterClient)
// Spawn everything you need
}
Your answer
Follow this Question
Related Questions
Photon in JavaScript? 1 Answer
Handle device offline status 0 Answers
Photon variable synchronization 1 Answer
RPC and inheritance 1 Answer