嗨,我開始在 Unity3d 中開發游戲,但我的代碼或檢查器中存在錯誤。
我的錯誤在陣列 arthursButtonsDescription 中,我在其中分配了 itemToEquip 兩次。第一回合一切順利 arthursButtonsDescription[0].name 是“0”,但第二回合我分配了 arthursButtonsDescription[4].name=4 并且 arthursButtonsDescription[0].name 也是 4,這是我的錯誤。
我錄制了有關此錯誤的視頻:https : //youtu.be/wZuFo5uhL5o
我將我的竊聽專案上傳到:https ://uloz.to/file/zfyxYuOiHTa4/dandd-zip#!ZGp4LmR2AzV0MTSvMGxkMwOvZwEuBGEuIRuhIyM6MJAgFGIuAj==/dandd-zip
using UnityEngine;
public class Item : MonoBehaviour
{
public Sprite icon = null;
public bool showInInventory = true;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Equipment : Item
{
public EquipmentSlot equipSlot;
new public string name = "New Item";
}
public enum EquipmentSlot { Head, Chest, Legs, Foot, Shield, Weapon,
Potion20Hp, Potion40Hp, Potion60Hp, Potion80Hp, Potion100Hp,
Potion25Percent, Potion50Percent, Potion75Percent, Potion100Percent }
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveLoadStarter : MonoBehaviour
{
public void LoadClick()
{
Equipment itemToEquip = gameObject.AddComponent<Equipment>();
itemToEquip.name = "0";
itemToEquip.equipSlot = EquipmentSlot.Head;
Debug.Log("iTE0: " itemToEquip.name);
EquipmentManager.instance.Equip(itemToEquip);
itemToEquip.name = "4";
itemToEquip.equipSlot = EquipmentSlot.Shield;
Debug.Log("iTE4: " itemToEquip.name);
EquipmentManager.instance.Equip(itemToEquip);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EquipmentManager : MonoBehaviour
{
public static EquipmentManager instance;
public void Awake()
{
if (instance != null)
{
Debug.Log("EquipmentManager instance not equals null");
return;
}
instance = this;
}
public ArthurInventory ai;
public void Equip(Equipment newItem)
{
ai = FindObjectOfType(typeof(ArthurInventory)) as ArthurInventory;
ai.ShowArthursButtons(newItem);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ArthurInventory : MonoBehaviour
{
public GameObject[] arthursButtons/* = new GameObject[6]*/;
public Equipment[] arthursButtonsDescription = new Equipment[6];
public void ShowArthursButtons(Equipment newItem)
{
for (int i = 0; i < arthursButtons.Length; i )
{
if (arthursButtons[i].name == "HellmButton" && newItem.equipSlot == EquipmentSlot.Head)
{
arthursButtons[i].GetComponentInChildren<Button>().GetComponentInChildren<Image>().enabled = true;
arthursButtonsDescription[0] = newItem;
}
if (arthursButtons[i].name == "ShieldButton" && newItem.equipSlot == EquipmentSlot.Shield)
{
arthursButtons[i].GetComponentInChildren<Button>().GetComponentInChildren<Image>().enabled = true;
arthursButtonsDescription[4] = newItem;
Debug.Log("AI0: " arthursButtonsDescription[0].name);
}
}
}
}
uj5u.com熱心網友回復:
看起來您的問題出在 SaveLoadStarter 中。
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveLoadStarter : MonoBehaviour
{
public void LoadClick()
{
Equipment itemToEquip = gameObject.AddComponent<Equipment>();
itemToEquip.name = "0";
itemToEquip.equipSlot = EquipmentSlot.Head;
Debug.Log("iTE0: " itemToEquip.name);
EquipmentManager.instance.Equip(itemToEquip);
itemToEquip.name = "4";
itemToEquip.equipSlot = EquipmentSlot.Shield;
Debug.Log("iTE4: " itemToEquip.name);
EquipmentManager.instance.Equip(itemToEquip);
}
}
您宣告 itemToEquip,設定它,然后再次設定它。
您需要創建一個新的設備物件。否則,您只是在更改第一個物件中的值。
嘗試這樣的事情:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveLoadStarter : MonoBehaviour
{
public void LoadClick()
{
Equipment itemToEquip = gameObject.AddComponent<Equipment>();
itemToEquip.name = "0";
itemToEquip.equipSlot = EquipmentSlot.Head;
Debug.Log("iTE0: " itemToEquip.name);
EquipmentManager.instance.Equip(itemToEquip);
itemToEquip = gameObject.AddComponent<Equipment>();
itemToEquip.name = "4";
itemToEquip.equipSlot = EquipmentSlot.Shield;
Debug.Log("iTE4: " itemToEquip.name);
EquipmentManager.instance.Equip(itemToEquip);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/347353.html
上一篇:使用回圈通過陣列將嵌套在內部的鍵值推送到不同的陣列中。Javascript
下一篇:選擇兩個陣列中的值
