I have a game with hundreds of unique prefab items that need to be randomly generated into the scene. The problem I’m running into is that I can’t seem to figure out how to prevent my spawner from creating duplicates from the array I’ve generated.
I wrote a bunch of if/else statements trying to compare my random array value but I ran into a chicken and the egg problem where I couldn’t compare a the array’s int because it hadn’t been instantiated yet and I couldn’t compare the int because it had already been instantiated.
I also tried to compare names and destroy, and search for the “Clone” tag and destroy (both seem round about - any guidance would be great
void Start()
{
myObjects = Resources.LoadAll<GameObject>("Prefabs");
startPosition = transform.position;
}
void SpawnRandomObject()
{
//spawns item in array between position number 0 thru 49
int whichItem = Random.Range (0, 49);
//replace gameObject set with script that gathers prefabs from folder
GameObject myObj = Instantiate (myObjects [whichItem]) as GameObject;
myObj.transform.position = transform.position;
}
//my efforts were something similar to this
void Awake()
{
var clones:GameObject = GameObject.FindWithTag("Clone"));
if(clones)
{
Destroy(clones.GameObject);
}
}
Thanks!