我正在使用一个数组来存储所有带有标签“ ChoosenItems”的游戏对象。如果该图层拾取了8个项目,它将打印出在控制台中选择的所有项目。当我拿起最后一个项目时,它会打印出前7个图像的名称,最后一个项目仍为“ empty_item”。它将所有元素添加到数组中,但是由于某种原因,最后一个元素不是最后选择的项目。
我尝试使用System.Threading.Thread.Sleep(5000)延迟我的功能;
public class PickUpItem : MonoBehaviour, IInteractable
{
public string DisplaySprite;
public string DisplayImage;
public static int counter;
public static GameObject InventorySlots;
public static GameObject[] PlayerItems = new GameObject[8];
public void Interact(DisplayImage currentDisplay)
{
ItemPickUp();
}
void Start() { }
void Update() { }
public void ItemPickUp()
{
InventorySlots = GameObject.Find("Slots");
int j;
counter = 0;
foreach (Transform slot in InventorySlots.transform)
{
if (slot.transform.GetChild(0).GetComponent<Image>().sprite.name == "empty_item")
{
slot.transform.GetChild(0).GetComponent<Image>().sprite =
Resources.Load<Sprite>("Inventory Items/" + DisplaySprite);
Destroy(gameObject);
break;
}
if (counter <= 7)
{
counter++;
if (counter >= 7)
{
{
Debug.Log("You have choosen all your items.");
System.Threading.Thread.Sleep(5000);
Debug.Log("Waiting 5 second.");
PlayerItems = GameObject.FindGameObjectsWithTag("ChoosenItem");
for (j = 0; j < 8; j++)
{
Debug.LogFormat("Item[{0}] = {1}", j, PlayerItems[j].GetComponent<Image>().sprite.name);
}
}
}
}
}
}
}
我希望输出为:
Item[0] = key_yellow
Item[1] = 2 spades
Item[2] = RocketSprite
Item[3] = color paper
Item[4] = 4 diamonds
Item[5] = CoinSprite
Item[6] = TruckChassisSprite
Item[7] = MagnetSprite
实际输出为:
Item[0] = key_yellow
Item[1] = 2 spades
Item[2] = RocketSprite
Item[3] = color paper
Item[4] = 4 diamonds
Item[5] = CoinSprite
Item[6] = TruckChassisSprite
Item[7] = empty_item
答案 0 :(得分:0)
您是否尝试逐步执行代码?处理完第一项counter
后,将设置为1。继续,处理完第七项后,counter
将被设置为7,并打印出所有项。
尝试更改
if (counter >= 7)
到
if (counter > 7)