Working on Multiplayer Game using photon pun 2 in Unity. I am trying to synchronize Box which is spawned when player trigger a function. So i need to synchronize game object for all players.enter image description here
Here is Spawning Codeenter image description here
private void SpawnBox()
{
PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs" , "PickableCube"), transform.position, Quaternion.identity);
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
SpawnBox();
}
}
I want to synchronize the game object for all players so if any player let the game it should not effect the game objects which are spawned in map . I tried PhotonNetwork instantiate , and also Photon view and photon view transform is applied on the box.
One issue I noticed is that there is no constraint on who runs the Instantiate function.
Imagine there are 2 players total and P1 enters the trigger.
That player enters the collider on each client.
P1 sees them enter and executes Instantiate.
P2 sees them enter and executes Instantiate.
Adding a local player check before spawning the box will prevent each client from instantiating the box.
private void SpawnBox()
{
if (!photonView.isMine) return; // Not our player, don't spawn box
PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs" , "PickableCube"), transform.position, Quaternion.identity);
}