Search code examples
c#unity-game-engineaugmented-reality

how can I set an amount of game objects to spawn?


here is my code and i have no idea how to work it up. im a beginner in c# and this is for our project. the project is about a balloon popper and i am having trouble to set an amount of balloons to spawn. I am planning to set the amount of balloons to spawn at 5, 10 even 20 and after that, the spawning will stop.

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

public class SpawnScript: MonoBehaviour
{
    public Transform[] spawnPoints;
    public GameObject[] balloons;

    public float spawnTime = 0f;
    float spawnTimeLeft = 0f;

    // Start is called before the first frame update
    void Update()
    {
        if (spawnTimeLeft >= spawnTime)
        {
            int randBalloon = Random.Range(0, balloons.Length);
            int randSpawnPoint = Random.Range(0, spawnPoints.Length);

            Instantiate(balloons[randBalloon], spawnPoints[randSpawnPoint].position, transform.rotation);
            spawnTimeLeft = 0f;
        }
        else
        {
            spawnTimeLeft = spawnTimeLeft + Time.deltaTime;
        }
    }
}

Solution

  • Add a variable to the class for the maximum amount. Have done this as public so you can set it in the inspector to the amount you wish.

    Also add a counter variable and set it to 0. This is private as nothing but this class needs access to it. Do serialize this field.

    public int spawnAmountMax = 5;
    [SerializeField]
    private int spawned = 0;
    

    Start the Update() with the next check, return from the method when it evaluates to true.

    if (spawned >= spawnAmountMax)
        return;
    

    Than after instantiating the GameObject, increase the variable:

    spawned++;
    

    The adjusted code

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
    
    public class SpawnScript : MonoBehaviour
    {
    
        public int spawnAmountMax = 5;
        private int spawned = 0;
    
        public Transform[] spawnPoints;
        public GameObject[] balloons;
    
        public float spawnTime = 0f;
        float spawnTimeLeft = 0f;
    
        // Start is called before the first frame update
        void Update()
        {
    
            if (spawned >= spawnAmountMax)
                return;
    
            if (spawnTimeLeft >= spawnTime)
            {
                int randBalloon = Random.Range(0, balloons.Length);
                int randSpawnPoint = Random.Range(0, spawnPoints.Length);
    
                Instantiate(balloons[randBalloon], spawnPoints[randSpawnPoint].position, transform.rotation);
                spawnTimeLeft = 0f;
    
                spawned++;
    
            } else
            {
                spawnTimeLeft = spawnTimeLeft + Time.deltaTime;
            }
        }
    }