【问题标题】:Array index out of bounds, couldnt find answer in other posts数组索引超出范围,在其他帖子中找不到答案
【发布时间】:2018-08-06 20:15:39
【问题描述】:

我正在使用 Blender 中的混合形状创建一个设备系统,但现在我添加了另一个混合形状,它不起作用。我收到此错误消息:

数组索引 (5) 超出范围 (size=5)

这是脚本中不起作用的部分:

public void UnequipAll()
{
    for (int i = 0; i < currentEquipment.Length; i++)
    {
        Unequip(i);
    }
    EquipDefaultItems();
}

void SetEquipmentBlendShapes(Equipment item, int weight)
{
    foreach (EquipmentMeshRegion blendShape in item.coveredMeshRegions)
    {
        targetMesh.SetBlendShapeWeight((int)blendShape, weight);
    }
}

第二个脚本:

public class Equipment : Item
{

    public EquipmentSlot equipSlot;
    public SkinnedMeshRenderer mesh;
    public EquipmentMeshRegion[] coveredMeshRegions;

    public int armorModifier;
    public int damageModifier;

    public override void Use()
    {
        base.Use();
        EquipmentManager.instance.Equip(this);
        RemoveFromInventory();
    }

}

public enum EquipmentSlot { Head, Chest, Legs, Weapon, Feet, Shield, Hands }
public enum EquipmentMeshRegion { Legs, Arms, Feet, Head, Chest, Hands };

【问题讨论】:

  • 会的。大小 5 意味着您可以使用索引 0 到 4。
  • 阅读the docs"Arrays are zero indexed: an array with n elements is indexed from 0 to n-1."
  • 对不起,我不太明白,但你知道我如何可以再添加一个索引,这样我就不会得到错误。对不起,如果我只是愚蠢,我的编码不太好,英语不是我的母语
  • 解决方案不是“再添加一个索引”到您的列表中,而是访问 正确 项。但是,很难猜测为什么会在不了解您的数据的情况下发生此错误。
  • 哪一行给出了错误?哪个数组的大小为 5?

标签: c# arrays unity3d indexing


【解决方案1】:

根据the documentationSetBlendShapeWeight() 依次为int indexfloat value

您的代码将EquipmentMeshRegion 转换为int,而不是传递实际索引。

尝试用常规的for 替换您的foreach 循环:

for(int index = 0; index < item.coveredMeshRegions.Length; index++)
{
    targetMesh.SetBlendShapeWeight(index, weight);
}

【讨论】:

  • 成功了,非常感谢,我整天都在努力解决这个问题。抱歉,如果我让这个问题变得比应该的更难,因为我说我的英语不是我的母语。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多