Extra prefabs getting spawned

I use the following code to spawn Players. But when its called both master and client there are extra prefabs getting
spawned. Can someone help me?
Extra 3 Target(clone) and Player(clone) are instantiated in the scene i.e 6 unnecessary clones
[PunRPC]
	public void RPC_SpawnTargets()
	{
		int i = 1;		
        TargetFormation=GameObject.Find("Target Formation").GetComponent<Transform>();
		foreach (Transform child in TargetFormation ) 
		{
			GameObject target = PhotonNetwork.Instantiate(Path.Combine("Prefabs","Target"),child.transform.position,Quaternion.identity,0) as GameObject;
			target.name = "Target ("+i+")";			
			target.transform.parent = child;
			print("Target Function called "+i+" times");			
			i++;
		}
		i = 0;		
	}

	[PunRPC]
	public void RPC_SpawnPlayers()
	{
		int i = 1;		
		PlayerFormation=GameObject.Find("Player Formation").GetComponent<Transform>();
		foreach (Transform child in PlayerFormation ) 
		{
			GameObject player =PhotonNetwork.Instantiate(Path.Combine("Prefabs","Player"),child.transform.position,Quaternion.identity,0) as GameObject;
			player.name = "Player ("+i+")";			
			player.transform.parent = child;		
			i++;
		}
		i=0;
	}


Functions are called in this order:
if (PlayersInGame==PhotonNetwork.playerList.Length)
			{
				
				PhotonView.RPC("RPC_SpawnPlayers",PhotonTargets.MasterClient);				
				PhotonView.RPC("RPC_SpawnTargets",PhotonTargets.MasterClient);			
				PhotonView.RPC("RPC_SpawnTargets",PhotonTargets.Others);					
				PhotonView.RPC("RPC_SpawnPlayers",PhotonTargets.Others);
			}

Answers

  • Try this
    if (PlayersInGame == PhotonNetwork.playerList.Length )
    {
    PhotonView.RPC("RPC_SpawnPlayers", PhotonTargets.AllBuffered);
    PhotonView.RPC("RPC_SpawnTargets",PhotonTargets.AllBuffered);
    }
Sign In or Register to comment.