Index was outside the bounds of the array

huangapple go评论93阅读模式
英文:

Unity | Index was outside the bounds of the array

问题

我需要一些用于画布中复制对象的随机整数。所以我创建了将填充随机值的数组,但它们不起作用,代码停在第一个数组行(CPU_Model),当我在Unity编辑器中查看数组时,它是空的(0个元素)。

抱歉,可能是我探索错误或类似的问题,我在编程方面是新手。

public int[] CPU_Model = new int[15];
public int[] CPU_MHz = new int[15];
public string[] CPU_Vars = new string[15];
public int i = 0;

public void Start()
{
    for (int i = 1; i < 15; i++)
    {
        Debug.Log(i);
        CPU_Model[i] = Random.Range(400, 9999);
        CPU_MHz[i] = Random.Range(1200, 5400);
        CPU_Vars[i] = "CPU" + i;
        //i++;
    }
    //i = 0;
}
英文:

I need some random int's for copied objects in canvas. So i created arrays that will fill with random values, but don't work and code stops on first array line (CPU_Model) and when i see array in unity editor it's empty (0 elements).
Sorry for wrong exploration or something like this, i'm new in coding.

 public int[] CPU_Model = new int[15];
    public int[] CPU_MHz = new int [15];
    public string[] CPU_Vars = new string[15];
    public int i = 0;

    public void Start()
    {
        for (int i = 1; i&lt;15; i++)
        {
            Debug.Log(i);
            CPU_Model[i] = Random.Range(400, 9999);
            CPU_MHz[i] = Random.Range(1200, 5400);
            CPU_Vars[i] = &quot;CPU&quot; + i;
            //i++;
        }
        //i = 0;
    }

答案1

得分: 1

在C#中,数组从索引0开始,而不是从1开始。
所以您应该将您的代码替换为

for (int i = 0; i<15; i++) {
  ....
}
英文:

In C# arrays start from index 0, not from 1.
So you should replace your code with

for (int i = 0; i&lt;15; i++) {
  ....
}

答案2

得分: 1

数组索引从0开始,因此如果你从1开始循环,你将跳过每个数组的第一个元素,它将未初始化。要解决这个问题,只需将循环初始化更改为从0开始,而不是从1开始,像这样:

for (int i = 0; i &lt; 15; i++)

此外,如果你在Start()方法内将i变量用作循环计数器,则无需将其声明为类成员变量。这个局部变量i会遮蔽类成员变量i,这意味着循环内部的i与循环外部的i是不同的变量。由于你只在循环内部使用局部变量i,并且不需要在循环外部访问其值,因此无需将其声明为类成员。

public int[] CPU_Model = new int[15];
public int[] CPU_MHz = new int[15];
public string[] CPU_Vars = new string[15];

public void Start()
{
    for (int i = 0; i &lt; 15; i++)
    {
        CPU_Model[i] = Random.Range(400, 9999);
        CPU_MHz[i] = Random.Range(1200, 5400);
        CPU_Vars[i] = &quot;CPU&quot; + i;
    }
}
英文:

Array indices start at 0, so if you start your loop at 1, you will skip the first element of each array, and it will be uninitialized. To fix this, simply change your loop initialization to start at 0 instead of 1, like this:

for (int i = 0; i &lt; 15; i++)

Also, it's not necessary to declare the i variable as a class member if you're using it as a loop counter inside the Start() method. This local variable i shadows the class member variable i, which means that the i inside the loop is a separate variable from the i outside the loop. Since you only use the local variable i inside the loop and don't need to access its value outside the loop, there is no need to declare it as a class member.

public int[] CPU_Model = new int[15];
public int[] CPU_MHz = new int[15];
public string[] CPU_Vars = new string[15];

public void Start()
{
    for (int i = 0; i &lt; 15; i++)
    {
        CPU_Model[i] = Random.Range(400, 9999);
        CPU_MHz[i] = Random.Range(1200, 5400);
        CPU_Vars[i] = &quot;CPU&quot; + i;
    }
}

答案3

得分: 0

已找到解决方案。数组不能是公共的,因此去掉它后系统可以正常工作。

英文:

Already found solution. Array can't be public, so without it all system works correctly.

huangapple
  • 本文由 发表于 2023年2月26日 21:44:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定