Question by
Milscodw · Oct 21, 2020 at 06:50 AM ·
collisiongameobjectinstantiate
How to spawn obstacles just after another one gets destroyed.
I want to destroy my old obstacle and spawn a new one.
I created a function called spawnObjects() on a separate script called spawn.cs .
public void spawnObjects() {
GameObject a = Instantiate(obstaclesPrefab) as GameObject;
}
Now in another script, I want to destroy the object when it collides with the player and take about 4 seconds to spawn an another one. I tried doing this, but it does not seem to work.
void OnTriggerEnter(Collider other)
{
Destroy(this.gameObject);
StartCoroutine(Waiting());
}
IEnumerator Waiting()
{
yield return new WaitForSeconds(4);
FindObjectOfType<spawn>().spawnObjects();
}
Comment