0
\$\begingroup\$

So I have this script that summons a stationary laser that the player is supposed to not get hit by, but since there's multiple game object with the code and they can all trigger at making it overwhelming to dodge I wanted to limit the amount that can be spawn at once (for example once 2 laser have activated a third one cant activate until one disapears)

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

public class enemyLazrWarning : MonoBehaviour
{
    public GameObject lazr;
    public GameObject lazrWarning;
    public Transform lazrPos;
    public movement player;
    public time waves;
    private float timer;
    private float m_FireTime = 1f;
    private float minFireDelay = 7f;
    private float maxFireDelay = 12f;
    private float warning;

    void Start()
    {
        this.m_FireTime = (float)Random.Range(this.minFireDelay, this.maxFireDelay);
        lazrWarning.gameObject.SetActive(false);
    }

    void Update()
    {
        if (player.gameStarted && waves.currentTime>0f)
        {
            timer += Time.deltaTime;    
            if(timer > m_FireTime) 
            {
                lazrWarning.gameObject.SetActive(true);
                warning += Time.deltaTime;
                if(warning > 2)
                {
                    timer -= this.m_FireTime;        
                    this.m_FireTime = (float)Random.Range(this.minFireDelay, this.maxFireDelay);
                    shoot();
                    lazrWarning.gameObject.SetActive(false);
                    warning = 0;
                }
            } 
        }
    }

    void shoot()
    {
        Instantiate(lazr, lazrPos.position, Quaternion.identity);
    }
}
```
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

I would solve this by having a central manager script that takes care of the total amount of things that can spawn per level. This has the advantage that each of your spawner scripts only needs to be aware of the manager and does not need to know about other spawners.

A manager is usually a good choice when you have some central logic or elements that are used by a lot of things. An example would be a tracker of your points or level progress. While you could store it as well as part of your player, things like level game logic (in your case total amount of lasers) does not really belong as part of your player.

I prefer a system of one MasterSingleton and task specific singletons that hold information/ logic for what they are needed. This is what the view in the inspector looks like, each child would be its own manager. There are other versions of singletons in far greater detail discussed here: In Unity, how do I correctly implement the singleton pattern?

Gamemanager SingleManager

public class MasterSingleton: MonoBehaviour {
    
    public static MasterSingleton Instance { get; private set; }
    public SpawnManager SpawnManager { get; private set; }
    public SoundManager TargetManager { get; private set; }
    
    private void Awake() {
        if (Instance != null && Instance != this) {
            Destroy(this);
            return;
        }
        Instance = this;
        SpawnManager = GetComponentInChildren<SpawnManager>();
        SoundManager = GetComponentInChildren<SoundManager>();
    }
}

public class SpawnManager: MonoBehaviour {

    public int maxLasersAllowed;
    public int currentLasersSpawned;
    public GameObject laserTurret;

    public void TryToSpawnLaser(Transform transform) {
        if (currentLasersSpawned< maxLasersAllowed) {
             Instantiate(laserTurret, transform.position, Quaternion.identity);
             currentLasersSpawned++;
        }    
    }

    public void ReduceTurrentCount() {
        currentLasersSpawned--;
    } 
}

The code for your shoot method would look like this now.

shoot() {
     MasterSingleton.Instance.SpawnManager.TryToSpawnLaser(transform);
}

When you destroy the laser, you should not forget to call the function for reducing the count as well. You could add some listeners / events to handle that as well.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .