Spawning multiple objects through a game manager in Unity

  Kiến thức lập trình

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

LEAVE A COMMENT