Question by
SeanSurtz · Apr 19, 2017 at 10:54 PM ·
instantiatewaitforsecondsfor-loop
IEnumerator and for loop not working [noob] What do I do?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovePipes : MonoBehaviour {
public GameObject pipes;
[SerializeField]
private float timeToSpawn;
[SerializeField]
private float maxPipes;
[SerializeField]
private float pipeSpeed;
[SerializeField]
private float maxX;
[SerializeField]
private Transform spawn;
[SerializeField]
private Transform rotation;
// Use this for initialization
void Start () {
StartCoroutine(TimeToSpawn());
}
// Update is called once per frame
void Update () {
transform.Translate(Vector3.left*Time.deltaTime*pipeSpeed);
if (transform.position.x < maxX)
{
Destroy(pipes);
}
}
IEnumerator TimeToSpawn()
{
for(int i = 0; i<maxPipes; i++)
{
Instantiate(pipes,spawn.position,rotation.rotation);
yield return new WaitForSeconds(timeToSpawn);
}
}
}
There's the code. I filled in the values in the inspector. I don't understand, it just keeps making the pipes over and over not even in the spawn position. It won't listen to what my max pipes are and it won't wait for the seconds either. Please help :c
Comment
@SeanSurtz Hi there, may we see the object details in the inspector?
Do any of these values change at runtime? also, why are you using a coroutine if this is only going to happen once? may as well call the method directly.