Spawning multiple objects through a game manager in Unity

I asked this on Unity discussions, but I figured I should ask here too in case anybody has advice.

I’m creating a 2D maze game where I have power ups and traps in place. I have multiple spawn points that go in an array for the script to choose from, so not random, and as soon as the player collides with a powerup or trap, I need it to go away as its been ‘used’ and spawn elsewhere on another spawn point. I figured out how to manage spawning a singular prefab, by directly specifiying it as a field in the inspector. Now I need to figure out how to make it work with multiple kinds of spawning objects that each have their own count of them there need to be at once in the scene (defined as initial count in the inspector and script).

I have an empty object in my game called game manager that essentially manages the overall aspects of the game such as saving and loading data, and also spawning different kinds of objects. I’ll put the script of one example of a prefab I need to spawn, a health buff, and the game manager script, this is how I’ve done it to make it for one specific spawnable object, now I need to do multiple by defining a general code for prefabs (maybe through an objects list I can add on the inspector? I don’t know how to do that though.) Any and all help is appreciated 🙂

Game Manager Script:

[Serializable]
public class GameManager : MonoBehaviour
{
    public GameObject prefab; // the prefab that should respawn 
    public Transform[] spawnPoints; // an array of the transform portion of the spawnpoints 
    public int initialPrefabCount; // the count that the prefab should maintain and starts at 
    public int currentPrefabCount; // the count that changes and is monitered 
    void Start() {
        // spawn the initial prefabs 
        for (int i = 0; i < initialPrefabCount; i++) {
             SpawnPrefab(); 
        }
    } 

    void SpawnPrefab() {
        if (currentPrefabCount >= initialPrefabCount) {
            return; // Limit reached, no need to spawn more prefabs
        }
        // Randomly select a spawn point from the array
        Transform spawnPoint = spawnPoints[UnityEngine.Random.Range(0, spawnPoints.Length)];

        // Instantiate the health buff prefab at the spawn point
        Instantiate(prefab, spawnPoint.position, Quaternion.identity);

        currentPrefabCount++; // Increase the count of the prefabs by one
    } 

    public void PrefabDestroyed() {
        currentPrefabCount--; // decrease the current count by one to show a prefab has been destroyed
        SpawnPrefab(); // call the function to spawn again 
    }
}

Health Buff Script:

public GameManager gameManager; 
  
void Start()
{ // checks whether any reference in the game editor is null
    if (gameManager == null) {
        Debug.LogWarning("GameManager reference is not assigned.");
        gameManager = FindObjectOfType<GameManager>(); // Find GameManager in the scene
    }
}


private void OnTriggerEnter2D(Collider2D collision) {
    // check if collision is with player or not
    if(collision.gameObject.tag == "Player"){
        Debug.Log("Player collided with health buff.");
        
        // collision data

        Destroy(gameObject); // destroyes the health buff
        // tells the game manager that a prefab has been destroyed and to spawn another  
        gameManager.PrefabDestroyed(); 
    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật