【发布时间】: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