want to kill the gameobjects when they leave the screen, and not to allow more than 7 to be in the screen at any given time. So no more than 7 at a time if on screen. I have this script which instantiates many and they float all over the world -
I want them to die when they leave the screen and the ones on screen can only be a max of 7 at a time
heres 1 of the codes I have on them
using UnityEngine;
using System.Collections.Generic;
// Starting in 2 seconds.
// a projectile will be launched every 0.3 seconds
public class sss : MonoBehaviour {
public GameObject prefab;
public Rigidbody2D projectile;
void Start() {
InvokeRepeating("LaunchProjectile", 1.0f, 6.0f);
Destroy (gameObject, 1.3f);
}
void LaunchProjectile () {
Rigidbody2D instance = Instantiate(projectile);
Destroy (gameObject, 1.3f);
instance.velocity = Random.insideUnitSphere * 11;
}
void SpawnItem()
{
// Instantiate the prefab somewhere between -10.0 and 10.0 on the x-z plane
Vector3 position = new Vector3(Random.Range(-10.0f, 10.0f), 0, Random.Range(-10.0f, 10.0f));
Instantiate(prefab, position, Quaternion.identity);
Destroy (gameObject, 2.3f);
}
}