Ways to Unity 16: Get all instantiated objects by specifying a prefab name

Why you need this 

Because sometimes, you want to know how many objects the user created during the runtime.
Or, if you prefer, you want to use a loop to handle a list of instantiated objects.

The code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnManager : MonoBehaviour
{
    private float spawnRange = 9;
    public GameObject enemyPrefab;
    public int enemyCount;

    void Start()
    {
        SpawnEnemyWave(1);
    }

    void Update()
    {
        enemyCount = FindObjectsOfType<Enemy>().Length;
        if (enemyCount == 0)
        {
            SpawnEnemyWave(1);
        }
    }

    void SpawnEnemyWave(int enemiesToSpawn)
    {
        for (int i = 0; i < enemiesToSpawn; i++)
        {
            Instantiate(enemyPrefab, GenerateRandomPosition(), enemyPrefab.transform.rotation);
        }
    }

    private Vector3 GenerateRandomPosition()
    {
        float spawnPosX = Random.Range(-spawnRange, spawnRange);
        float spawnPosZ = Random.Range(-spawnRange, spawnRange);
        Vector3 randomPos = new Vector3(spawnPosX, 0, spawnPosZ);
        return randomPos;
    }
}

Say hi to the original link

https://yingshaoxo.blogspot.com/2020/12/ways-to-unity-16-get-all-instantiated.html