- Home /
spawn an object using yield/startCoroutine
I'm trying to get my head around yield()
and startCoroutine()
.
I currently have an object moving in a direction on screen, I have spawn points set up around the scene and I would like an object to spawn from these points every 5 seconds. (I'm just using one for now as a reference)
The problem is, the console never goes into the functions I have setup for this, I get no console output. Code below:
using UnityEngine;
using System.Collections;
public class spawn : MonoBehaviour
{
// Prefab to spawn
public int distance = 10;
public int timeDelay = 5;
public float speed = 0.1f;
public Transform clone;
public GameObject spawnPoint;
void Update()
{
spawnDisc();
transform.position += transform.forward * distance * speed;
}
IEnumerator spawnDisc()
{
//Never gets here
print("Gotten to spawnDisc");
yield return StartCoroutine(myDelayMethod(timeDelay));
Instantiate (clone, spawnPoint.transform.position, spawnPoint.transform.rotation);
}
IEnumerator myDelayMethod(int delay)
{
//or here
yield return new WaitForSeconds (delay);
print ("Gotten to myDelayMethod");
}
}
I've only just started using the yield/startCoroutine functions so I'm trying to wrap my head around it but if someone could tell me where I'm going wrong, I'd appreciate it
Answer by ChrisSch · Jul 17, 2014 at 09:12 PM
Wow wow wow hold up a sec. xD You don't need to call a coroutine twice if you only have one 5 sec delay. Also I don't think you can call a coroutine every frame inside of update, or it will run instantly.
But yours probably isn't working at all because you're calling it like spawnDisc();
, I think it should be StartCoroutine("spawnDisc");
and also having two IEnumerators for one coroutine isn't needed.
Here's a simplest version of it.
IEnumerator SpawnDelay()
{
Debug.Log("Happens before");
yield return new WaitForSeconds(5);
Debug.Log("Happens after");
}
void Start()
{
StartCoroutine("SpawnDelay");
}
If I'm right and you can't call a coroutine inside Update every frame, you'll have to use booleans or something to check its only called one frame.
But another way, the one I'm using for spawning atm, is without coroutines.
Goes something like this:
public float spawnTime = 5;
private float curSpawnTime;
void Start()
{
curSpawnTime = spawnTime;
}
void Update()
{
curSpawnTime-=1 * Time.deltaTime;
if(curSpawnTime <= 0)
{
Spawn();
curSpawnTime = spawnTime;
}
}
void Spawn();
{
//Your spawning code
}
Looking at yours seems so much simpler :P I think I just overwhelmed myself with all of this ha. I'll implement this and see how i get on, cheers Chris
It's clear I haven't a clue about Coroutines ha :P You're way was much simpler, thanks for the help
Anytime! :) And don't worry about it, you'll get there. Wait till you start dealing with Quaternions. xD
Answer by purpl3grape · Jul 27, 2016 at 04:56 PM
Hi, I actually was working on this in my project. So here is my solution. First:
1) requestRespawnPlayer is a bool
2) Paste this and modify where necessary
3) @ Start() implement. Set the last yield to a reasonable interval you want it to repeat by
IEnumerator PlayerRespawnCoroutine(){
while (true) {
if(requestRespawnPlayer){
yield return new WaitForSeconds (2f);
SpawnMyPlayer(playerPrefab.name, _teamSelID, classSelID);
requestRespawnPlayer = false;
}
yield return new WaitForSeconds(.2f);
}
}