【问题标题】:Isn't Unity Transform[] a dynamic array?Unity Transform[] 不是动态数组吗?
【发布时间】:2022-12-18 20:18:55
【问题描述】:

IndexOutOfRangeException:索引超出数组范围。 (包装器 stelemref)System.Object.virt_stelemref_class_small_idepth(intptr,对象)

启动Unity已经一周了,Unity Transform[]是静态数组,不是不直接初始化的动态递增数组?到目前为止我认为它是一个动态数组,但是我应该将它声明为一个动态数组的类型是什么?

我总是得到这个错误,因为我认为它是一个动态数组。如何提前防止此错误?

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    在 C# 中,包含括号的数组是具有固定长度的静态数组。

    Transform[] trns = new Transform[10]
    

    如果要在运行时增加数组的大小,可以使用 new 关键字来创建更大的数组。

    Transform[] trns = new Transform[30]
    

    我建议使用“列表”。因为列表是一个动态数组,所以可以在执行的时候调整数组的长度。

    List<Transform> list = new List<Transform>();
    

    【讨论】:

      猜你喜欢
      • 2023-01-03
      • 2017-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-05
      • 2015-03-15
      • 2014-02-10
      • 2021-11-29
      相关资源
      最近更新 更多