using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour {
public Transform [] SpawnLocations;
public GameObject[] WhatToSpawnPrefab;
public GameObject[] WhatToSpawnClone;
void Start(){
SpawnSomethingAwesomePlease ();
}
void SpawnSomethingAwesomePlease(){
WhatToSpawnClone[0] = Instantiate(WhatToSpawnPrefab[0],SpawnLocations[0].transform.position,Quaternion.Euler(0,0,0)) as GameObject;
WhatToSpawnClone[1] = Instantiate(WhatToSpawnPrefab[1],SpawnLocations[1].transform.position,Quaternion.Euler(0,0,0)) as GameObject;
WhatToSpawnClone [2] = Instantiate(WhatToSpawnPrefab [2], SpawnLocations[2].transform.position, Quaternion.Euler (0, 0, 0)) as GameObject;
}
}
There’s two ways to do this. You can either use a coroutine, or you can use a timer in your Update function:
private float timer = 0F;
private const float TIME_INTERVAL = 2F;
void Update()
{
this.timer += Time.deltaTime;
// Time.deltaTime is the amount of time in seconds that has elapsed
// since the previous Update step. Update often runs around 60 times per second,
// so deltaTime is typically very small (1/60 of a second = 0.0167F)
if (this.timer >= TIME_INTERVAL)
{
this.SpawnSomethingAwesomePlease();
this.timer = 0F;
}
}
I think the Unity’s function MonoBehaviour.InvokeRepeating is intended to do just that.
void Start(){
InvokeRepeating("SpawnSomethingAwesomePlease", 0.5f, 2.0f);
}
And you can stop the routine anytime with CancelInvoke("SpawnSomethingAwesomePlease")