Hello everyone.
Im having some difficulty with a script.
Here is what im trying to accomplish:
-I have a game object called spawner. Attached to the spawner is a spawner script that will spawn a publicly defined gameobject. The spawner has variables to control the time delay between spawned objects, the total spawn amount and the current spawned amount.
I am having difficulty keeping track of spawned objects however. Here is the code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public int spawnAmount;
private int currentSpawned;
public int allowedSpawned;
public float spawnDelay;
float timer;
public GameObject spawnType;
Vector3 myPos;
// Start is called before the first frame update
void Start()
{
currentSpawned = 0;
myPos = this.transform.position;
timer = spawnDelay;
}
// Update is called once per frame
void Update()
{
timer -= Time.deltaTime;
if(currentSpawned < allowedSpawned && timer <= 0) //having difficulty counting the active spawned objects
{
Instantiate(spawnType, myPos, this.transform.rotation);
timer = spawnDelay;
spawnAmount--;
}
if (spawnAmount <= 0)
Destroy(gameObject);
}
}
Now I am aware of counting game objects with tags. However I have different enemy prefabs that share the same tag “Enemy”, so the script is including all objects with the same tag which is not what I want.
Is there a way in unity to count the game objects that are instantiated by this script?
Any help is greatly appreciated.
Thank you