I want to delete the clone when it gets to the end of the screen
here’s my code :
public class SpawnEnemy : MonoBehaviour
{
public GameObject enemy;
public float respawnTime = 1.0f;
private Vector2 screenBounds;
private float nextSpawn = 0.0f;
private float randX;
private Vector2 whereToSpawn;
public Transform Enemy;
// Start is called before the first frame update
void Start()
{
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
}
// Update is called once per frame
void Update()
{
if(Time.time > nextSpawn)
{
nextSpawn = Time.time + respawnTime;
randX = Random.Range(screenBounds.x - 2, screenBounds.x * -1 + 2);
whereToSpawn = new Vector2(randX, transform.position.y);
Instantiate(enemy, whereToSpawn, Enemy.rotation);
}
if (Enemy.transform.position.y > screenBounds.y * -1)
{
Destroy(Instantiate(enemy, whereToSpawn, Enemy.rotation));
}
}