【发布时间】:2016-08-10 16:43:23
【问题描述】:
我想将array 变量中的数字洗牌一次,所以我从Start() 调用了我的Shuffle 方法。
然后我尝试从更新中访问改组后的数组,但我无法这样做。我怎样才能访问它?有没有其他方法可以将我的数组洗牌一次,然后永远使用它?
private System.Random _random = new System.Random();
float sec;
float timecount;
float starttime;
void Start () {
starttime = Time.time;
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8};
Shuffle(array);
foreach (int value in array)
{
Debug.Log(value);
}
}
void Update () {
//time
timecount = Time.time - starttime;
sec = (timecount % 60f);
if (Mathf.Round(sec) == 3f) {
//access shuffled array
Debug.Log(array[3]); <=====error here
}
}
//for shuffle number from array
void Shuffle(int[] array){
int p = array.Length;
for (int n = p-1; n > 0 ; n--)
{
int r = _random.Next(1, n);
int t = array[r];
array[r] = array[n];
array[n] = t;
}
}
【问题讨论】: