How to get script to execute only once in room

Options
I am trying to make a script that spawns three capture points in random locations in the map when the game starts (before the game starts players wait in a lobby).

However, my spawn script (the script that spawns the three objects in random locations) gets executed whenever a player switches to the room scene, instead of executing only at the very start of the game.

The script uses Photon.MonoBehaviour. I have also tried using custom room properties to define whether or not the objects have already spawned, but nothing that depends on network sync seems to work (maybe because it is too slow?).

What is a solution for this? All I want is for this script to execute one time once the scene has been loaded for the first time and that's it.

Comments

  • M4TT
    Options
    Hello.
    You should try :
    
    if(PhotonNetwork.isMasterClient){
       Photonview.RPC("SetSpawn", PhotonTargets.All);
    }
    
    [PunRPC]
    private void SetSpawn(){
       spawn1.transform.position
       spawn2.transform.position
       spawn3.transform.position
    }
    The script must be on the game scene.
    So when the master client have load the scene, he told all the others that the position of the spawn have changed.
  • ZeroByter
    Options
    M4TT said:

    Hello.
    You should try :

    
    if(PhotonNetwork.isMasterClient){
       Photonview.RPC("SetSpawn", PhotonTargets.All);
    }
    
    [PunRPC]
    private void SetSpawn(){
       spawn1.transform.position
       spawn2.transform.position
       spawn3.transform.position
    }
    The script must be on the game scene.
    So when the master client have load the scene, he told all the others that the position of the spawn have changed.
    Thanks! This worked for me.

    I always had the mindset that the objects should be ready the instant the first player joins, but I realize that's not the case, since I can always wait for all the players to join first at the start of the game or something.
  • M4TT
    Options
    Yep, in the case that you dont want to wait all players before the game start you can simply change to PhotonTargets.AllBuffered