且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Unity中随机排列数组

更新时间:2023-11-18 23:01:16

当前,您的数组是在 Start()函数中声明的.这意味着只能从 Start()函数中对其进行访问.如果希望能够同时从 Start() Update()访问该数组,则需要通过全局声明来扩大其范围.例如:

Currently, your array is declared within your Start() function. This means it can only be accessed from within the Start() function. If you want to be able to access the array from both Start() and Update() you need to increase its scope by declaring it globally. For example:

private System.Random _random = new System.Random();
float sec;
float timecount;
float starttime;

//declare array globally
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8};

void Start () 
{
  starttime = Time.time;
  Shuffle(array);
  foreach (int value in array)
  {
      Debug.Log(value);
  }
}

void Update () 
{
    //now you can access array here, because it is declared globally
}

此外,请参阅此MSDN文章有关范围的信息C#.

Additionally, have a look at this MSDN article about scope in C#.