using System.Collections; using System.Collections.Generic; using UnityEngine; public class TargetSpawner : MonoBehaviour { public int xRange = 30; public int yRange = 30; public GameObject targetPrefab; private List targetList = new(); // Start is called before the first frame update void Start() { SpawnTarget(); } private void SpawnTarget(bool spawnRandom = true, Vector3 spawnPosition = new Vector3()) { int xPos = (int)spawnPosition.x; int yPos = (int)spawnPosition.y; if (spawnRandom) { xPos = Random.Range(-xRange, xRange); yPos = Random.Range(-yRange, yRange); } var target = Instantiate(targetPrefab); target.transform.SetParent(transform); targetList.Add(target); target.transform.localPosition = new Vector3(xPos, yPos, 0); target.GetComponent().spawner = gameObject.GetComponent(); } void Update() { if (targetList.Count < 1) { SpawnTarget(); } } public void TargetDestroyed(GameObject go) { targetList.Remove(go); } }