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);
}
}
}
You rather want to "shuffle" the array once and then iterate the shuffled array e.g. using Linq
OrderBy
and usingRandom.value
as order like