using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.VisualScripting;
using UnityEngine;
public class SpawnerAsteroids : MonoBehaviour
{
public Vector2 backgroundScale = new Vector2(240, 120); // The scale of your background
[SerializeField] int asteroidLimit = 50;
[SerializeField] public List<GameObject> asteroidsOnMap;
[SerializeField] public GameObject[] spawnersArr;
// Start is called before the first frame update
void Start()
{
}
void Update()
{
if (asteroidsOnMap.Count < asteroidLimit)
instantiateAsteroid();
}
private void instantiateAsteroid()
{
Debug.Log("Number of spawners available: " + spawnersArr.Length);
if (spawnersArr.Length > 0)
{
GameObject spawnerChosen = spawnersArr[Random.Range(0, spawnersArr.Length)];
if (spawnerChosen != null && spawnerChosen.GetComponent<Spawner>() != null)
{
spawnerChosen.GetComponent<Spawner>().canSpawn = true;
asteroidsOnMap.Add(spawnerChosen.GetComponent<Spawner>().instantiatedAsteroid);
Debug.Log("Asteroid instantiated.");
}
else
{
Debug.LogError("SpawnerChosen is null or does not have a Spawner component.");
}
}
else
{
Debug.LogError("Spawner array is empty.");
}
}
}
Hey everyone, i need some help with something here… I have 34 game objects in my level that are used for spawners. I’m trying to add these objects to an array of objects in a script so I can administer those spawns. I’m using a SerializeField GameObject[ ] in the script and adding the objects in Unity Editor. The issue is that if I print the length of the array when the game runs, it shows as empty… Any ideas why? I’ll show the script I have and the Inspector.