I gave 18 gameobjects that I want too set to active through script. the problem is that it will be inefficient to write a script and set each one o the 18 gameobjects to true or false. so is there a way to do this using an array and randomly set them to active ??
This is so easy.
public GameObject[] ObjectsList; // Not only 18, this script will except any number of game objects you put here.
void Start() {
ObjectsList[Random.Range(0,ObjectsList.Length)].SetActive(true);
}
using System.Linq; // Top of the script, outside of the class
public GameObject[] arrayOfGameObjects;
// Randomly activates an inactive game object
public void ActivateRandomObject()
{
GameObject selection = arrayOfGameObjects
.Where(i=>!i.activeSelf)
.OrderBy(n=>Random.value).FirstOrDefault();
// selection will be null if all game objects are already active
if (selection != null) selection.SetActive(true);
}
Thanks goes to dubbreak at Select random object in list using linq
Tnx man
Thank you very much !
It worked for me to set active ramdom gameobjects