using System.Collections; using System.Collections.Generic; using UnityEngine; public class CrateSpawn : MonoBehaviour { List cratePool; int index = -1; public GameObject cratePrefab; public float spawnTime = 4f; public float speed = 35; public int poolSize = 5; public bool isRunning = true; [HideInInspector] public bool hit = false; void Start() { cratePool = new List(); for (int i = 0; i < poolSize; i++) { var crate = (GameObject)Instantiate(cratePrefab, new Vector3(0, 8, 0), Quaternion.identity); cratePool.Add(crate.GetComponent()); } StartCoroutine(SpawnCrate()); } void Update() { for (int i = 0; i < poolSize; i++) { var crate = cratePool[i]; crate.transform.position += new Vector3(0, speed * Time.deltaTime, 0); if (crate.hit) { hit = true; } } } IEnumerator SpawnCrate() { while (isRunning) { index++; index %= poolSize; var crate = cratePool[index]; float x = Random.Range(-2, 2); crate.transform.position = new Vector3(x, -6, 0); yield return new WaitForSeconds(spawnTime); } } }