I’m new to unity, I am trying to create a system on my 2d game that will spawn in prefabs randomly, then will delete them randomly depending on if there are more than two prefabs on the screen. The randomly spawning in was pretty easy, but I am struggling with the random deletion. I have tried a few different ways, but best I have found assigning the spawned in prefabs to a list then delete items from the list by a tag. I’ve had it run where it works for a bit then the objects quit getting destroyed. My guess is that once I delete an object from the list the rest aren’t shifting down. Any suggestions are helpful, as my current code is pretty sloppy.
Anything commented out are things I was trying but didn’t workout. The swipe portion is a swipe listener package for my mobile game FYI. Thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GG.Infrastructure.Utils.Swipe;
public class Spawner : MonoBehaviour
{
[SerializeField] private SwipeListener swipeListener;
//string[] stringArray = new string[3];
public CharacterScript character;
public GameObject[] PREFs;
public List<GameObject> PREFSLIST = new List<GameObject>();
// ^said static before
private int counter;
private int reoccurenceCounter;
private GameObject destroyedReoccurence;
private GameObject destroyed;
private int nextFree;
private void OnEnable()
{
swipeListener.OnSwipe.AddListener(OnSwipe);
//PREFs = new GameObject[3];
counter = 0;
reoccurenceCounter = 0;
}
private void OnSwipe(string swipe)
{
/*PREFs[0] = GameObject.FindGameObjectWithTag("PREF0");
PREFs[1] = GameObject.FindGameObjectWithTag("PREF1");
PREFs[2] = GameObject.FindGameObjectWithTag("PREF2"); */
int randomIndex = Random.Range(0, PREFs.Length);
//GameObject clone = Instantiate(PREFs[randomIndex], transform.position, Quaternion.identity);
if (randomIndex == 0)
{
GameObject clone = Instantiate(PREFs[randomIndex], new Vector2(-1.78f, 1.13f), Quaternion.Euler(0, 0, 180));
PREFSLIST.Add(GameObject.FindGameObjectWithTag("PREF0"));
counter++;
reoccurenceCounter++;
}
else if (randomIndex == 1)
{
GameObject clone = Instantiate(PREFs[randomIndex], new Vector2(0.91f, -1.34f), Quaternion.identity);
//int randomIndex2 = Random.Range(0, PREFs.Length);
//Destroy(gameObject);
PREFSLIST.Add(GameObject.FindGameObjectWithTag("PREF1"));
counter++;
reoccurenceCounter++;
}
else if (randomIndex == 2)
{
GameObject clone = Instantiate(PREFs[randomIndex], new Vector2(-0.72f, -0.28f), Quaternion.Euler(0, 0, 90));
PREFSLIST.Add(GameObject.FindGameObjectWithTag("PREF2"));
counter++;
reoccurenceCounter++;
}
if (reoccurenceCounter > 2)
{
//Destroy(PREFs[0]);
destroyedReoccurence = PREFSLIST[0];
Destroy(destroyedReoccurence);
PREFSLIST.Remove(destroyedReoccurence);
Debug.Log(PREFSLIST.Count);
}
//string newtag = (PREFSLIST[Random.Range(0, PREFSLIST.Count)]);
/*if (counter > 2)
{
destroyed = PREFSLIST[Random.Range(0, PREFSLIST.Count)];
Destroy(destroyed);
PREFSLIST.Remove(destroyed);
Debug.Log(PREFSLIST.Count);
}*/
//Debug.Log(counter);
/*foreach (var item in PREFSLIST) {
var bruh = PREFSLIST.Count;
if (PREFSLIST.Count != PREFSLIST.Distinct.Count()){
Destroy(item);
}
} */
//Debug.Log(PREFSLIST);
//(PREFSLIST[Random.Range(0, PREFSLIST.Count)]);
}