【问题标题】:Array error in C# for UnityUnity 的 C# 中的数组错误
【发布时间】:2014-04-21 08:44:36
【问题描述】:

大家好,我想在 C# 中创建一个清单以实现统一,但是当我拿起我的物品时遇到了一个数组错误,我不知道为什么我想知道是否有人可以提供帮助。我到处搜索了一些提示,但没有遇到任何错误:-

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

作为编辑附上完整的代码

代码附在下面:-

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

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

【问题讨论】:

  • 请使用 unity3d 标签。 unity 标签是完全不同的东西。
  • 从您的代码看来,您正在创建一个数组列表对象,添加 Transform 项,然后在新数组上调用 ToBuiltIn 方法,然后将新数组的内容复制到它的内置版本。我不太了解 Unity SDK,但在我看来这是一件很奇怪的事情。

标签: c# arrays unity3d inventory


【解决方案1】:

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

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

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

【讨论】:

  • 好的,非常感谢,我正在寻找并尝试调试代码,实际上整个早上都在尝试,到目前为止,lolz
  • 只需进行一些测试和一些消息,以便您了解发生了什么。主要问题是您的目标数组(内置统一数组)的容量设置低于您需要的容量。
  • 不,真的卡住了,我将在下面附上我的完整代码,我可能需要重建它
  • 现在看起来很简单我认为您没有初始化 Contents 变量。所以它总是一个长度为零的数组。
  • 我将如何初始化该变量?我以为我在这里做了? public Transform[] 内容;
猜你喜欢
  • 2016-10-12
  • 1970-01-01
  • 1970-01-01
  • 2012-11-12
  • 1970-01-01
  • 2023-03-03
  • 2017-04-11
  • 2018-12-23
  • 1970-01-01
相关资源
最近更新 更多