How do I restrict one random prefab to be used only once but placed randomly with a bunch of prefabs of arrays on top of other object?
using System.Collections.Generic;
using UnityEngine;
public class LevelRoomsScript : MonoBehaviour
{
[SerializeField]
private GameObject[] memberWoodArray = null;
[SerializeField]
private GameObject[] memberRoomPrefabArray = null;
void Start()
{
foreach (GameObject localWood in memberWoodArray)
{
int localNumRoomPrefab = memberRoomPrefabArray.Length;
int localRoomIndex = Random.Range(0, localNumRoomPrefab);
GameObject localRoomPrefab = memberRoomPrefabArray[localRoomIndex];
Instantiate(localRoomPrefab, localWood.transform.position, Quaternion.identity);
}
}
}
The way I understand your question is that you want to instantiate each element in memberRoomPrefabArray at most once. You could create a temporary list that is a copy of memberRoomPrefabArray and remove each element that is instantiated before the next loop cycle.
You might want to add some checks like
if (temp.Count == 0) { break; }if it's possible formemberRoomPrefabArrayto be shorter thanmemberWoodArray.Edit: Changed
Random.Range(0, temp.Count - 1)toRandom.Range(0, temp.Count)since, apparently, it's only maximally inclusive with floats and not integers.