using UnityEngine; public class TrashSpawner : MonoBehaviour { public GameObject[] objectsToSpawn; public float timeTillStart; public float timeToRepeat; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { InvokeRepeating("spawnTrash", timeTillStart, timeToRepeat); } void spawnTrash() { float minX = gameObject.transform.position.x - gameObject.transform.localScale.x / 2; float maxX = gameObject.transform.position.x + gameObject.transform.localScale.x / 2; float Y = gameObject.transform.position.y; float randomX = Random.Range(minX, maxX); Vector2 spawnPosition = new Vector2(randomX, Y); Instantiate(objectsToSpawn[Random.Range(0, objectsToSpawn.Length)], spawnPosition, Quaternion.identity); } }