Kill gameobjects when they leave the screen

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);
	}


}

Hi there! What you want here is called Pooling. There’s a great unity tutorial on it Here. Pooling is a CPU efficient system for objects that you would rapidly instantiate and destroy (such as bullets or en mass enemys). The link above also contains a script which pools only a certain number of objects, for example 7, as you needed.

To destroy an object as it leaves the screen, there are several options. If the game is 2D, the best way in my opinion is to set up a collision box around the edges of the camera. If your camera moves around, simply make them a child of the camera so it moves with them. If it’s in 3d, you can access a boolean Renderer.IsVisible, which does exactly what you think it does.

public Renderer myRenderer; //Drag reference here
if (!myRenderer.IsVisble)
{
    gameObject.SetActive(false);
}