Hello, I am trying to implement a spawner object and am looking for some advice about the best approach in dealing with multiple timers.
Here is a stripped down example of what I am doing, in c#:
[System.Serializable]
public class SpawnerList
{
public Transform objectToSpawn;
public float secondsUntilNextSpawn = 1;
}
public class Spawner : MonoBehaviour
{
public SpawnerList[] spawnerList;
}
So in the inspector you have an array of SpawnerList objects, each having a prefab and a spawn time. What would be the best approach to dealing with tracking those different times, and spawning the different prefabs at the right times?
If each "SpawnerList"-item (which should have a singular name like SpawnItem) should be spawned independently, just start a seperate coroutine for each item.
edit: changed my example
[System.Serializable]
public class SpawnItem
{
public Transform objectToSpawn;
public float secondsUntilNextSpawn = 1;
}
public class Spawner : MonoBehaviour
{
public SpawnItem[] spawnList;
IEnumerator SpawnLoop(SpawnItem Item)
{
while(true) // spawns infinite
{
Instantiate(Item.objectToSpawn);
yield return new WaitForSeconds(Item.secondsUntilNextSpawn);
}
}
void Start()
{
foreach (SpawnItem I in spawnList)
StartCoroutine(SpawnLoop(I));
}
}
Or which is propably much simpler, attach as many scripts as needed to a GameObject. Since every item works on it's own just create a MonoBehaviour which handles a single object and just attach as many you need:
public class Spawner : MonoBehaviour
{
public Transform objectToSpawn;
public float secondsUntilNextSpawn = 1;
IEnumerator Start()
{
while(true) // spawns infinite
{
Instantiate(objectToSpawn);
yield return new WaitForSeconds(secondsUntilNextSpawn);
}
}
// If you want to stop the spawn loop when the script get disabled, just add this:
void OnDisable()
{
StopAllCoroutines();
}
}
Start a coroutine inside each object…
[System.Serializable]
public class SpawnerItem
{
public Transform objectToSpawn;
public float m_spawnDelay = 1;
Spawner m_spawner;
bool m_canRun;
public void Start(Spawner p_spawner)
{
m_spawner = p_spawner;
m_canRun = true;
p_spawner.StartCoroutine(SelfRun());
}
public void Stop()
{
m_canRun = false;
}
IEnumerator SelfRun()
{
while(m_canRun)
{
yield return new WaitForSeconds(m_spawnDelay);
Debug.Log("Im running every..." + m_spawnDelay);
}
}
}
public class Spawner : MonoBehaviour
{
public SpawnerItem[] m_spawnerItems;
void Start()
{
if(m_spawnerItems == null)
return;
foreach(SpawnerItem eachItem in m_spawnerItems)
{
if(eachItem == null)
continue;
eachItem.Start(this);
}
}
}