Shuffle array in Unity

前端 未结 3 2060
执念已碎
执念已碎 2021-01-27 04:30

I want to shuffle the numbers in my array variable once, so I called my Shuffle method from Start().

I then attempt access the sh

3条回答
  •  不思量自难忘°
    2021-01-27 04:49

    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
    }
    

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

提交回复
热议问题