Unity中C#中的数组错误

时间:2014-04-21 08:44:37

标签: c# arrays unity3d inventory

嘿大家我想在C#中制作一个库存清单,但是当我拿起我的物品时我遇到一个数组错误,我无法弄清楚为什么我想知道是否有人可以提供帮助。我到处寻找一些提示,但没有遇到任何错误: -

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

编辑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);
}
}

希望有人可以提前帮助你:)

1 个答案:

答案 0 :(得分:0)

当阵列仅分配模拟阵列交互具有自动增长功能的复杂类型时,阵列具有固定大小。

检查您正在复制原始阵列内容的目标阵列的容量。

我不确定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);
    }
}