I am trying to make the game detect when there is less than one of a certain game object, it will spawn after a time. I cant use tags for it, as the object and its variant i am not wanting to spawn at this point both have the same tags. I am trying to use a list but i really don’t know how to use it, so if someone could tell me how to use it OR give me a better solution, that would be appreciated. Here is my code, the code i am trying to use is at the bottom, and I’m calling it in the Cow Spawn method. I’d appreciate explaining what code is being given to me as well!
using System.Collections.Generic;
using NUnit.Framework;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UIElements;
using static UnityEngine.Rendering.DebugUI.Table;
public class SpawnManager : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
public GameObject[] cowPrefab;
private PlayerControl PlayerControl;
public GameObject Plane;
PointTracker pointTracker;
GameObject Cow;
CreatureMovement creatureMovement;
public List<GameObject> cows;
void Start()
{
Cow = GameObject.Find("Cow(Clone)");
pointTracker = GameObject.Find("PointTracker").GetComponent<PointTracker>();
PlayerControl = GameObject.Find("UFO").GetComponent<PlayerControl>();
creatureMovement = GetComponent<CreatureMovement>();
InvokeRepeating("CowSpawn", 1, 5);
InvokeRepeating("PlaneSpawn", 1, Random.Range(10,15));
}
// Update is called once per frame
void Update()
{
{
}
}
void CowSpawn()
{
if (PlayerControl.gameOver == false && pointTracker.score < 10)
{
int randomNumber = Random.Range(0, 2);
float angle = randomNumber == 0 ? 90f : -90f;
Vector3 spawnPosition = new Vector3(Random.Range(-11f, 11f), 1.5f, -1.39f);
Instantiate(cowPrefab[Random.Range(0, cowPrefab.Length)], spawnPosition, Quaternion.Euler(0, angle, 0));
}
if (PlayerControl.gameOver == false && pointTracker.score >= 10 && pointTracker.score < 24)
{
int randomNumber = Random.Range(0, 2);
float angle = randomNumber == 0 ? 90f : -90f;
Vector3 spawnPosition = new Vector3(Random.Range(-11f, 11f), 1.5f, -1.39f);
Instantiate(cowPrefab[Random.Range(0, 3)], spawnPosition, Quaternion.Euler(0, angle, 0));
}
if(PlayerControl.gameOver == false && pointTracker.score >= 24)
{
StartCoroutine(CreaturePhase());
}
}
void PlaneSpawn()
{
if (pointTracker.score < 8)
{
float yRangeDown = 8.0f;
float yRangeUp = 9.8f;
int randomNumber = Random.Range(0, 2);
float angle = randomNumber == 0 ? 90f : -90f;
Vector3 spawnRange = new Vector3(31.3f, Random.Range(yRangeDown, yRangeUp), -1);
Vector3 spawnRangeLeft = new Vector3(-31.3f, Random.Range(yRangeDown, yRangeUp), -1);
if (PlayerControl.gameOver == false)
{
GameObject newPlane = Instantiate(Plane, spawnRange, Quaternion.Euler(0, angle, 0));
if (angle == 90)
{
newPlane.transform.position = spawnRangeLeft;
}
}
}
}
public IEnumerator CreaturePhase()
{
if (cows == null && creatureMovement.creaturePhase == true)
{
yield return new WaitForSeconds(5);
Vector3 spawnPosition = new Vector3(Random.Range(-11f, 11f), 1.5f, -1.39f);
int randomNumber = Random.Range(0, 2);
float angle = randomNumber == 0 ? 90f : -90f;
Instantiate(cowPrefab[Random.Range(0, 3)], spawnPosition, Quaternion.Euler(0, angle, 0));
}
}
}