我试图用 for 循环中生成的 1-3 中的随机数开始我的场景。
我打算让数字每次都是随机的,但是我不希望直接生成相同的两个数字,而是首先生成一个 1-3 之间的随机数,然后等待 60 秒,生成一个随机数1-3 之间的数字,不包括最近生成的数字。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KOTH_ZoneRandom : MonoBehaviour
{
int Rand;
int Lenght = 4;
List<int> list = new List<int>();
void Start()
{
list = new List<int>(new int[Lenght]);
for (int j = 1; j < Lenght; j++)
{
Rand = Random.Range(1, 4);
while (list.Contains(Rand))
{
Rand = Random.Range(1, 4);
}
list[j] = Rand;
print(list[j]);
}
}
}
Edit尝试实现一个协同例程来充当循环的间隔。但是它仍然不起作用,它会打印到控制台,因此协同例程肯定正在执行,但是 WaitForSeconds 函数似乎不起作用。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KOTH_ZoneRandom : MonoBehaviour
{
int Rand;
int Length = 4;
List<int> list = new List<int>();
void Start()
{
list = new List<int>(new int[Length]);
for (int j = 1; j < Length; j++)
{
StartCoroutine(loopDelay());
Rand = Random.Range(1, 4);
while (list.Contains(Rand))
{
Rand = Random.Range(1, 4);
}
list[j] = Rand;
print(list[j]);
}
}
IEnumerator loopDelay ()
{
print("started delay");
yield return new WaitForSeconds(60);
}
}
Start 方法可以是协程,因此将返回类型更改为IEnumerator
.
Add yield return new WaitForSeconds(60);
在 for 循环的末尾。
IEnumerator Start()
{
list = new List<int>(new int[Lenght]);
for (int j = 1; j < Lenght; j++)
{
Rand = Random.Range(1, 4);
while (list.Contains(Rand))
{
Rand = Random.Range(1, 4);
}
list[j] = Rand;
print(list[j]);
yield return new WaitForSeconds(60);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)