【发布时间】:2016-10-30 05:04:03
【问题描述】:
我有一个简单的问题要问。只需检查下面的代码,问题就在代码的结果中。我希望你们能帮助我..
文件inventory.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class inventory : MonoBehaviour {
public List<item> tmp = new List<item> ();
public item itema;
itemDatabase database;
// Use this for initialization
void Start () {
int slotAmount = 0;
database = GameObject.FindGameObjectWithTag ("itemDatabase").GetComponent<itemDatabase> ();
//Generate the Slot and Slot Name;
for(int i = 1; i <= 24; i++) {
tmp.Add (new item());
}
itema = database.items [0];
tmp [0] = itema;
tmp [0].itemStock = 1;
Debug.Log (tmp[0].itemID + " - " + tmp[0].itemStock);
itema = database.items [0];
tmp [1] = itema;
tmp [1].itemStock = 2;
Debug.Log (tmp[1].itemID + " - " + tmp[1].itemStock);
itema = database.items [0];
tmp [2] = itema;
tmp [2].itemStock = 3;
Debug.Log (tmp[2].itemID + " - " + tmp[2].itemStock);
Debug.Log (tmp[0].itemID + " - " + tmp[0].itemStock);
Debug.Log (tmp[1].itemID + " - " + tmp[1].itemStock);
}
}
结果是:
tmp Array [0] : 1 Stock 1
tmp Array [1] : 1 Stock 2
tmp Array [2] : 1 Stock 3
tmp Array [0] : 1 Stock 3 // this array recently stock was 1 now replace with 3 Why it is replace i have use an array ?
tmp Array [1] : 1 Stock 3 // this array recently stock was 2 now replace with 3 Why it is replace i have use an array ?
问题在 tmp Array[0] 和 tmp Array[1] 上,它被 tmp Array[2] 替换。
谢谢 丹尼斯
【问题讨论】:
-
请去掉不必要的代码,您会得到更好的响应
标签: c# arrays class unity3d unityscript