How to limit entity count in-game?

So i want to limit the entitys in game to 50 but i don’t know how to.
Here’s my code if necessary:

//this is the var i used
private GameObject[] getCount;

//here i reference the var and make a new one using the first's length
void Start()
    {
        getCount = GameObject.FindGameObjectsWithTag("Enemy");
        count = getCount.Length;
    }
// i thinked that this if would limit but i don't know what to put
if (count > 50)
        {

        }

Im also new to Unity and it if is any other wrong thing say it too!

Kinda depends on what you want.

Destroy all of them if there are more than 50?

Destroy random ones until there is exactly 50?

Destroy the closest ones until there is exactly 50?

Something else?

You need to define that first.

Also, it’s always going to be better to simply control it BEFORE they spawn.

Add references to each entity to a collection (such as a List) and use that to decide “do I already have enough? then don’t spawn any new ones.”

I want to prevent more enemys from spawning

You can do:

for(int i = 0, i < 50, i++)
{
GameObject go = new Gameobject(“GameObject_” + i);
}

Or you use Instantiate instead of new GameObject. The documentation has an example for exactly your use-case: Unity - Scripting API: Object.Instantiate

You better add limit in script which is spawning enemies so you never have situation with enemies count above limit.

In that case, I’m curious why you didn’t post your spawning code. As Lekret notes above, that’s the obvious place to put your logic.

I use instantiate for spawning enemies

In whichever script you have that’s instantiating your enemies, make it keep track of how many have been instantiated, and prevent it from going over a maximum amount.
Example:

public class SpawnerExample : MonoBehaviour
{
  public GameObject prefab;
  public int maxSpawnCount = 50;

  //The list of all the GameObjects we spawned.
  private List<GameObject> instances = new List<GameObject>();

  public void Spawn()
  {
    //Only spawn a new GameObject if we have less than maxSpawnCount instances.
    if(instances.Count < maxSpawnCount)
    {
      GameObject instance = Instantiate(prefab);
      instances.Add(instance);
    }
  }
}

You may also run into an edge case where if a spawned GameObject gets destroyed, its reference will become null in the instances List, but the Count of the List will not decrease, meaning you won’t be able to spawn any more instances even though you should be able to.

To fix that, you can update the List before every time you want to spawn a new instance to get rid of any null references:

public class SpawnerExample : MonoBehaviour
{
  public GameObject prefab;
  public int maxSpawnCount = 50;

  //The list of all the GameObjects we spawned.
  private List<GameObject> instances = new List<GameObject>();

  public void Spawn()
  {
    //Remove all instances in the list that are null.
    instances.RemoveAll(instance => instance == null);

    //Only spawn a new GameObject if we have less than maxSpawnCount instances.
    if(instances.Count < maxSpawnCount)
    {
      GameObject instance = Instantiate(prefab);
      instances.Add(instance);
    }
  }
}