Unity的C#中的数组错误

本文关键字:错误 数组 Unity | 更新日期: 2023-09-27 17:59:06

大家好,我正试图在C#中为unity制作一个库存列表,但当我拿起我的物品时遇到了一个数组错误,我不明白为什么我想知道是否有人能帮忙。我到处寻找一些提示,但没有遇到任何错误是:-

ArgumentException:目标数组不够长。检查destIndex和长度,以及数组的下限

作为编辑iv附上完整的代码

附在下面的代码:-

using UnityEngine;
using System.Collections;

[AddComponentMenu ("Inventory/Inventory")]
public class Inventory : MonoBehaviour {
//This is the central piece of the Inventory System.
public Transform[] Contents; //The content of the Inventory
public int MaxContent = 12; //The maximum number of items the Player can carry.
bool DebugMode = true; //If this is turned on the Inventory script will output the base of what it's doing to the Console window.
private InventoryDisplay playersInvDisplay; //Keep track of the InventoryDisplay script.
public Transform itemHolderObject; //The object the unactive items are going to be parented to. In most cases this is going to be the Inventory object itself.

//Handle components and assign the itemHolderObject.
void Awake (){
    itemHolderObject = gameObject.transform;
    playersInvDisplay = GetComponent<InventoryDisplay>();
    if (playersInvDisplay == null)
    {
        Debug.LogError("No Inventory Display script was found on " + transform.name + " but an Inventory script was.");
        Debug.LogError("Unless a Inventory Display script is added the Inventory won't show. Add it to the same gameobject as the Inventory for maximum performance");
    }
}
//Add an item to the inventory.
public void AddItem ( Transform Item  ){
    ArrayList newContents = new ArrayList();
    //FIXME_VAR_TYPE newContents= new Array(Contents);
    newContents.Add(Item);
    //Contents=newContents.ToBuiltin(Transform); //Array to unity builtin array
    newContents.CopyTo(Contents); //Array to unity builtin array
    System.Array.Resize(ref Contents, 1);
    if (DebugMode)
    {
        Debug.Log(Item.name+" has been added to inventroy");
    }
    //Tell the InventoryDisplay to update the list.
    if (playersInvDisplay != null)
    {
        playersInvDisplay.UpdateInventoryList();
    }
}
//Removed an item from the inventory (IT DOESN'T DROP IT).
public void RemoveItem ( Transform Item  ){
        ArrayList newContents = new ArrayList();
    //FIXME_VAR_TYPE newContents=new Array(Contents); //!!!!//
    int index = 0;
    bool shouldend = false;
    foreach(Transform i in newContents) //Loop through the Items in the Inventory:
    {
        if(i == Item) //When a match is found, remove the Item.
        {
            newContents.RemoveAt(index);
            shouldend=true;
            //No need to continue running through the loop since we found our item.
        }
        index++;
        if(shouldend) //Exit the loop
        {
            //Contents=newContents.ToBuiltin(Transform); //!!!!//
            Contents=newContents.ToArray(typeof (Transform)) as Transform[];
            if (DebugMode)
            {
                Debug.Log(Item.name+" has been removed from inventroy");
            }
            if (playersInvDisplay != null)
            {
                playersInvDisplay.UpdateInventoryList();
            }
            return;
        }
    }
}
//Dropping an Item from the Inventory
public void DropItem (Item item){
    gameObject.SendMessage ("PlayDropItemSound", SendMessageOptions.DontRequireReceiver); //Play sound
    bool makeDuplicate = false;
    if (item.stack == 1) //Drop item
    {
        RemoveItem(item.transform);
    }
    else //Drop from stack
    {
        item.stack -= 1;
        makeDuplicate = true;
    }
    item.DropMeFromThePlayer(makeDuplicate); //Calling the drop function + telling it if the object is stacked or not.
    if (DebugMode)
    {
        Debug.Log(item.name + " has been dropped");
    }
}
//This will tell you everything that is in the inventory.
void DebugInfo (){
        Debug.Log("Inventory Debug - Contents");
    int items=0;
    foreach(Transform i in Contents){
        items++;
        Debug.Log(i.name);
    }
    Debug.Log("Inventory contains "+items+" Item(s)");
}
//Drawing an 'S' in the scene view on top of the object the Inventory is attached to stay organized.
void OnDrawGizmos (){
    Gizmos.DrawIcon (new Vector3(transform.position.x, transform.position.y + 2.3f, transform.position.z), "InventoryGizmo.png", true);
}
}

希望有人能提前帮上忙谢谢你:)

Unity的C#中的数组错误

当数组被分配时,它们的大小是固定的——只有模拟数组交互的复杂类型才具有自动增长功能。

检查要复制原始数组内容的目标数组的容量。

我不确定ToBuiltIn方法的作用,但它必须创建一个新数组作为原始数组的副本(顺便说一句,您的数据可能已经在数组中了,请通过调试进行检查),其容量设置为数组中的项目总数,可以是1或0。

检查变量并调试代码以查看数组的长度和容量。

更新(根据更新的代码)

调用Add Item方法时,Contents数组未初始化。

为了清楚起见,我已经重写了你粘贴的代码,我没有你使用的所有类,所以我没有测试或检查它的构建,但它会让你知道你能做什么。如果你使用泛型列表,你的数组管理可以简化。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[AddComponentMenu ("Inventory/Inventory")]
public class Inventory : MonoBehaviour {
    //This is the central piece of the Inventory System.
    public List<Transform> Contents; //The content of the Inventory
    public int MaxContent = 12; //The maximum number of items the Player can carry.
    bool DebugMode = true; //If this is turned on the Inventory script will output the base of what it's doing to the Console window.
    private InventoryDisplay playersInvDisplay; //Keep track of the InventoryDisplay script.
    public Transform itemHolderObject; //The object the unactive items are going to be parented to. In most cases this is going to be the Inventory object itself.
    public Inventory()
    {
        this.Contents = new List<Transform> (MaxContent);
    }
    //Handle components and assign the itemHolderObject.
    void Awake (){
        itemHolderObject = gameObject.transform;
        playersInvDisplay = GetComponent<InventoryDisplay>();
        if (playersInvDisplay == null)
        {
            Debug.LogError("No Inventory Display script was found on " + transform.name + " but an Inventory script was.");
            Debug.LogError("Unless a Inventory Display script is added the Inventory won't show. Add it to the same gameobject as the Inventory for maximum performance");
        }
    }
    //Add an item to the inventory.
    public void AddItem ( Transform Item  ){
        if (this.Contents.Count < this.MaxContent) {
                        Contents.Add (Item);
                        if (DebugMode) {
                                Debug.Log (Item.name + " has been added to inventroy");
                        }
                        //Tell the InventoryDisplay to update the list.
                        if (playersInvDisplay != null) {
                                playersInvDisplay.UpdateInventoryList ();
                        }
                } else {
                    // show message that inventory is full
                }
    }
    //Removed an item from the inventory (IT DOESN'T DROP IT).
    public void RemoveItem ( Transform Item  ){
        if (this.Contents.Remove (Item)) {
                        if (DebugMode) {
                                Debug.Log (Item.name + " has been removed from inventroy");
                        }
                        if (playersInvDisplay != null) {
                                playersInvDisplay.UpdateInventoryList ();
                        }
                        return;
                } else {
                    // Item is not in inventory
                }
    }
    //Dropping an Item from the Inventory
    public void DropItem (Item item){
        gameObject.SendMessage ("PlayDropItemSound", SendMessageOptions.DontRequireReceiver); //Play sound
        bool makeDuplicate = false;
        if (item.stack == 1) //Drop item
        {
            RemoveItem(item.transform);
        }
        else //Drop from stack
        {
            item.stack -= 1;
            makeDuplicate = true;
        }
        item.DropMeFromThePlayer(makeDuplicate); //Calling the drop function + telling it if the object is stacked or not.
        if (DebugMode)
        {
            Debug.Log(item.name + " has been dropped");
        }
    }
    //This will tell you everything that is in the inventory.
    void DebugInfo (){
        Debug.Log("Inventory Debug - Contents");
        int items=0;
        foreach(Transform i in Contents){
            items++;
            Debug.Log(i.name);
        }
        Debug.Log("Inventory contains "+items+" Item(s)");
    }
    //Drawing an 'S' in the scene view on top of the object the Inventory is attached to stay organized.
    void OnDrawGizmos (){
        Gizmos.DrawIcon (new Vector3(transform.position.x, transform.position.y + 2.3f, transform.position.z), "InventoryGizmo.png", true);
    }
}