Home > Back-end >  how can I set an amount of game objects to spawn?
how can I set an amount of game objects to spawn?

Time:12-15

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

CodePudding user response:

Try this code:

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

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

    public float spawnTime = 5.0f;
    public float maxSpawnTime = 20.0f;
    
    private void Start()
    {
        Invoke(nameof(Spawn), spawnTime); //This will start the spawning process after the initially set spawnTime, so first this will wait for 5 seconds.
    }
    
    private void Spawn()
    {
        int randBalloon = Random.Range(0, balloons.Length);
        int randSpawnPoint = Random.Range(0, spawnPoints.Length);

        Instantiate(balloons[randBalloon], spawnPoints[randSpawnPoint].position, transform.rotation);
        
        spawnTime *= 2; //doubles the spawnTime, according to your request, so the second time this will be 10, and the third time, this will be 20 seconds
        if (spawnTime <= maxSpawnTime) //check if we reached or not the maximum spawn time
        {
            Invoke(nameof(Spawn), spawnTime); //if we not yet reached the maximum spawn time, it will start to wait and spawn one more balloon again.
        }
    }
}

CodePudding user response:

since you're doing this in you update, you can add a counter after your Instantiate call.

    Instantiate(balloons[randBalloon], spawnPoints[randSpawnPoint].position, transform.rotation);
    counter  ;

And then, nest your if statement inside another if checking for the counter.

   if(counter < amountToSpawn)
   {
      //your code here
   }

For me personally, I would make a Coroutine method with delay that will have a for loop inside, rather than doing this in update.

Sample:

private IEnumerator SpawnObjects(int spawnCount)
{
   for(int i = 0; i < spawnCount; i  )
   {
      //Instantiate here
      yield return new WaitForSeconds(5);
   }
}

To call the method:

StartCoroutine(SpawnObjects(10));

CodePudding user response:

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;
        }
    }
}
  • Related