I have already been able to spawn my cubes in a row. Now I want to create a small delay so you can see the cubes individually falling. I have tried making a coroutine where it waits, and I have read multiple of these qustion forums and nothing has worked. I was very confident in the code below, but that does not work as well. Any help would be amazing.
using UnityEngine;
using System.Collections;
public class CubeSpawn1 : MonoBehaviour
{
public Vector3 ObjectSpawnPosition;
public GameObject obj;
private float nextSpawn;
public float fireRate;
public float NumBlks;
public float Depth;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey("s") && Time.time > nextSpawn)
{
nextSpawn = Time.time + fireRate;
for (int j = 0; j < Depth; ++j)
{
for (int i = 0; i < NumBlks; ++i)
{
Instantiate(obj, ObjectSpawnPosition, Quaternion.identity);
ObjectSpawnPosition.x += 1;
StartCoroutine(HERE());
}
ObjectSpawnPosition.x -= NumBlks;
ObjectSpawnPosition.z += 1;
}
ObjectSpawnPosition.z -= NumBlks;
}
}
public IEnumerator HERE()
{
float x = 1;
yield return new WaitForSeconds(1.0f); //this stops the coroutine for 1 second.
x += 1;
}
}
This should work, I have only redited it in text editor so I am not 100% sure:
// Update is called once per frame
void Update()
{
if (Input.GetKey("s") && Time.time > nextSpawn)
{
nextSpawn = Time.time + fireRate;
for (int j = 0; j < Depth; ++j)
{
StartCoroutine(HERE());
ObjectSpawnPosition.x -= NumBlks;
ObjectSpawnPosition.z += 1;
}
ObjectSpawnPosition.z -= NumBlks;
}
}
public IEnumerator HERE()
{
for (int i = 0; i < NumBlks; ++i)
{
Instantiate(obj, ObjectSpawnPosition, Quaternion.identity);
ObjectSpawnPosition.x += 1;
yield return new WaitForSeconds(1.0f);
}
}
In short, you shoult not use a coroutine as a “wait timer”, your coroutine has to do the spawning and waiting itself. Your task is basically to run it.
You can change this probably to have both of your loops inside the coroutine and all you have to do is run it after “nextSpawn” increase.
Thank you! That almost worked I just had to do some small tweaking but I got it to work. I really appreciate the help.
You could set the instantiated objects to non-active and add them to a queue, then activate them successively. You can compute the time between activations like you do for the initial spawning. This approach would look like this:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CubeSpawn1 : MonoBehaviour
{
public Vector3 ObjectSpawnPosition;
public GameObject Obj;
private float nextBulk = 0.0f;
private float nextSpawn = 0.0f;
public float FireRate;
public float TimeBetweenSpawns;
private Queue<GameObject> objectsToSpawn;
public int NumBlks;
public int Depth;
// Use this for initialization
void Start()
{
this.objectsToSpawn = new Queue<GameObject>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKey("s") && Time.time > nextBulk)
{
nextBulk = Time.time + FireRate;
for (int j = 0; j < Depth; ++j)
{
for (int i = 0; i < NumBlks; ++i)
{
GameObject curObj = Instantiate<GameObject>(Obj);
curObj.transform.position = ObjectSpawnPosition;
curObj.transform.localRotation = Quaternion.identity;
curObj.SetActive(false);
this.objectsToSpawn.Enqueue(curObj);
ObjectSpawnPosition.x += 1;
}
ObjectSpawnPosition.x -= NumBlks;
ObjectSpawnPosition.z += 1;
}
ObjectSpawnPosition.z -= NumBlks;
}
if(Time.time>nextSpawn && this.objectsToSpawn.Count>0)
{
nextSpawn = Time.time + TimeBetweenSpawns;
this.objectsToSpawn.Dequeue().SetActive(true);
}
}
}
Thank you! That almost worked I just had to do some small tweaking but I got it to work. I really appreciate the help.
– DLewIVWhat the log says?
– Dakwamine