W
W
WasTabon2020-09-21 19:18:22
C++ / C#
WasTabon, 2020-09-21 19:18:22

Why does random selection of an element from an array not work?

Here is the code itself, it seems to include a random object, but for some reason, everything in turn

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LevelPlay : MonoBehaviour
{
    public bool isDead = false;
    public GameObject[] OnButtons;
    public bool IsStart = false;

    IEnumerator LevelStart()
    {
        while (isDead == false)
        {
            yield return new WaitForSeconds(0.5f);
            int rnd = Random.Range(0, 215);
            OnButtons[rnd].SetActive(true);
        }
    }

    private void Start()
    {
        StartCoroutine(LevelStart());
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
yuopi, 2020-09-22
@yuopi

var random = new Random(DateTime.Now.Millisecond);
int rnd = random.Next(OnButton.Length);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question