A
A
ArcanumXt2017-09-15 06:54:56
C++ / C#
ArcanumXt, 2017-09-15 06:54:56

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

2 answer(s)
S
Stanislav Silin, 2017-09-15
@ArcanumXt

public void ActivateRandomObject(GameObject[] arrayOfGameObjects)
 {
     arrayOfGameObjects[Random.Range(0, arraOfGameObjects.Length - 1)].SetActive(true);
 }

E
Evgeny Kalibrov, 2017-09-15
@rework

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 question

Ask a Question

731 491 924 answers to any question