Hey, I have a problem, i'm trying to make an object spawner. I use the follow script :
The problem is: If a client connect to the server AFTER the first wave of spawn (for exemple), he will not see the objects on the ground but the players connected before will. How can I make them show on the client's screen after their connection ?
using UnityEngine;
using System.Collections;
public class LootManager : MonoBehaviour {
private int i;
public string[] prefabName;
public Transform[] spawnPoints;
public int amountItems = 1;
public int yieldTimeMin = 20;
public int yieldTimeMax = 30;
void Start () {
////////////////////////////////////////////////////
/// Spawn 3 Items at the beggining of the server
///////////////////////////////////////////////////
//First Item To Spawn
if(PhotonNetwork.offlineMode) {
string Startobj = prefabName[Random.Range(0, prefabName.Length)];
Transform Startpos = spawnPoints[Random.Range(0, spawnPoints.Length)];
Instantiate(Resources.Load(Startobj, typeof(GameObject)), Startpos.position, Startpos.rotation);
} else {
if(PhotonNetwork.isMasterClient) {
string Startobj = prefabName[Random.Range(0, prefabName.Length)];
Transform Startpos = spawnPoints[Random.Range(0, spawnPoints.Length)];
PhotonNetwork.Instantiate(Startobj, Startpos.position, Startpos.rotation, 0);
}
}
//Second Item To Spawn
if(PhotonNetwork.offlineMode) {
string Startobj2 = prefabName[Random.Range(0, prefabName.Length)];
Transform Startpos2 = spawnPoints[Random.Range(0, spawnPoints.Length)];
Instantiate(Resources.Load(Startobj2, typeof(GameObject)), Startpos2.position, Startpos2.rotation);
} else {
if(PhotonNetwork.isMasterClient) {
string Startobj2 = prefabName[Random.Range(0, prefabName.Length)];
Transform Startpos2 = spawnPoints[Random.Range(0, spawnPoints.Length)];
PhotonNetwork.Instantiate(Startobj2, Startpos2.position, Startpos2.rotation, 0);
}
}
//Third Item To Spawn
if(PhotonNetwork.offlineMode) {
string Startobj3 = prefabName[Random.Range(0, prefabName.Length)];
Transform Startpos3 = spawnPoints[Random.Range(0, spawnPoints.Length)];
Instantiate(Resources.Load(Startobj3, typeof(GameObject)), Startpos3.position, Startpos3.rotation);
} else {
if(PhotonNetwork.isMasterClient) {
string Startobj3 = prefabName[Random.Range(0, prefabName.Length)];
Transform Startpos3 = spawnPoints[Random.Range(0, spawnPoints.Length)];
PhotonNetwork.Instantiate(Startobj3, Startpos3.position, Startpos3.rotation, 0);
}
}
//////////////////////////////////
/// Start the spawning coroutine
/////////////////////////////////
StartCoroutine(SpawnItems());
}
IEnumerator SpawnItems () {
while(true) {
for(i=0; i<amountItems; i++) {
yield return new WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));
//First Item To Spawn
if(PhotonNetwork.offlineMode) {
string Startobj = prefabName[Random.Range(0, prefabName.Length)];
Transform Startpos = spawnPoints[Random.Range(0, spawnPoints.Length)];
Instantiate(Resources.Load(Startobj, typeof(GameObject)), Startpos.position, Startpos.rotation);
} else {
if(PhotonNetwork.isMasterClient) {
string Startobj = prefabName[Random.Range(0, prefabName.Length)];
Transform Startpos = spawnPoints[Random.Range(0, spawnPoints.Length)];
PhotonNetwork.Instantiate(Startobj, Startpos.position, Startpos.rotation, 0);
}
}
//Second Item To Spawn
if(PhotonNetwork.offlineMode) {
string Startobj2 = prefabName[Random.Range(0, prefabName.Length)];
Transform Startpos2 = spawnPoints[Random.Range(0, spawnPoints.Length)];
Instantiate(Resources.Load(Startobj2, typeof(GameObject)), Startpos2.position, Startpos2.rotation);
} else {
if(PhotonNetwork.isMasterClient) {
string Startobj2 = prefabName[Random.Range(0, prefabName.Length)];
Transform Startpos2 = spawnPoints[Random.Range(0, spawnPoints.Length)];
PhotonNetwork.Instantiate(Startobj2, Startpos2.position, Startpos2.rotation, 0);
}
}
//Third Item To Spawn
if(PhotonNetwork.offlineMode) {
string Startobj3 = prefabName[Random.Range(0, prefabName.Length)];
Transform Startpos3 = spawnPoints[Random.Range(0, spawnPoints.Length)];
Instantiate(Resources.Load(Startobj3, typeof(GameObject)), Startpos3.position, Startpos3.rotation);
} else {
if(PhotonNetwork.isMasterClient) {
string Startobj3 = prefabName[Random.Range(0, prefabName.Length)];
Transform Startpos3 = spawnPoints[Random.Range(0, spawnPoints.Length)];
PhotonNetwork.Instantiate(Startobj3, Startpos3.position, Startpos3.rotation, 0);
}
}
}
}
}
}
The problem is: If a client connect to the server AFTER the first wave of spawn (for exemple), he will not see the objects on the ground but the players connected before will. How can I make them show on the client's screen after their connection ?