0

I am spawning objects on startup,(maxObj = 75) then destroying Obj's on event and disabling spawner Obj. When player wants they can re enable spawner. I need count to start at 0 on enable. Another 75 obj's spawn and are then destroyed. etc. Appreciate any help thanks.

enter code here
 private static readonly float _spawnTime = 0.125f;

    [SerializeField]

    private GameObject _asteroidObject = null;

    [SerializeField]
    private int _maxObjects = 0;

    private int _spawnedObjects = 0;

    private float _time = 0;

    private void Update()
    {
        if(_spawnedObjects < _maxObjects)
        {
            if(_time > _spawnTime)
            {
                Instantiate(_asteroidObject, transform.position, Quaternion.identity);
                ++_spawnedObjects;
                _time = 0;
            }
            _time += Time.smoothDeltaTime;
        }
    }
Vix
  • 9
  • 2

2 Answers2

0

Touch it is quite unclear how a User shall be able to start the spawn again I would recommend to rather use a Coroutine in general. They are like little temporary Update blocks but easier to control and maintain. It is also more efficient since it doesn't call the Update method every frame also when the maxObjects amount is already reached and nothing is going to happen anyway

[SerializeField] 
private GameObject _asteroidPrefab;

[SerializeField] 
private float _spawnInterval = 0.125f;

[SerializeField] 
private int _maxObjects;

private bool _canStart = true;

// However your player calls this method
public void StartSpawn()
{
    // Only start spawning if there is no other spawn routine already running
    if(_canStart) StartCoroutine(Spawn());
}

private IEnumerator Spawn()
{
    // Just in case ignore if another routine is already running
    if(!_canStart) yield break;

    // block concurrent routines
    _canStart = false;

    var interval = new WaitForSeconds(_spawnInterval);

    for(var i = 0; i < _maxObjects; i++)
    {
        Instantiate(_asteroidObject, transform.position, Quaternion.identity);

        // yield makes Unity pause this routine and render the frame
        // and continue from here in the next frame
        // Then since we yield return another IEnumerator in thi case WaitForSconds
        // it results exactly in this: Holds here for given seconds and then goes to the next iteration 
        yield return interval;
    }

    // re-enable the StartSpawn
    _canStart = true;
}

Then in case you additionally also want to automatically start spawning in the beginning you can simply call it in

private void Start()
{
    StartSpawn();
} 
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Fantastic works like a treat. 2 minor adjustments for anybody wanting to use this – Vix Mar 27 '20 at 20:09
0
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnObj : MonoBehaviour
{
    [SerializeField]
    private GameObject _asteroidPrefab;

    [SerializeField]
    private float _spawnInterval = 0.125f;

    [SerializeField]
    private int _maxObjects;

    private bool _canStart = true;

    private void Start()
    {
        StartSpawn();
    }
    // However your player calls this method
    public void StartSpawn()
    {
        // Only start spawning if there is no other spawn routine already running
        if (_canStart) StartCoroutine(Spawn());
    }

    private IEnumerator Spawn()
    {
        // Just in case ignore if another routine is already running
        if (!_canStart) yield break;

        // block concurrent routines
        _canStart = false;

        var interval = new WaitForSeconds(_spawnInterval);

        for (var i = 0; i < _maxObjects; i++)
        {
            Instantiate(_asteroidPrefab, transform.position, Quaternion.identity);

            // yield makes Unity pause this routine and render the frame
            // and continue from here in the next frame
            // Then since we yield return another IEnumerator in thi case WaitForSconds
            // it results exactly in this: Holds here for given seconds and then goes to the next iteration 
            yield return interval;
        }
        // re-enable the StartSpawn
        _canStart = true;
    }
}
Vix
  • 9
  • 2