Answer the question
In order to leave comments, you need to log in
How to run objects randomly in Unity3D?
Hello! How can you include objects in Unity3D randomly? That is, there are 5 objects, all of them are deactivated, you need to make sure that one of the five objects is randomly launched when the scene starts. Language - C#. Thanks in advance!
Answer the question
In order to leave comments, you need to log in
public void ActivateRandomObject(GameObject[] arrayOfGameObjects)
{
arrayOfGameObjects[Random.Range(0, arraOfGameObjects.Length - 1)].SetActive(true);
}
public GameObject[] arrayOfGameObjects;
// Активируем случайный объект
public void ActivateRandomObject()
{
GameObject selection = arrayOfGameObjects
.Where(i=>!i.activeSelf)
.OrderBy(n=>Random.value).FirstOrDefault();
if (selection != null) selection.SetActive(true);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question