Во время выполнения я сначала добавляю некоторые элементы в массив, затем записываю их значения в журнал (работает просто отлично), затем я создаю их экземпляры (все еще работает нормально), а затем я хочу изменить значения элементов массива, но для некоторых почему теперь индексы массива возвращают null? Кто-нибудь, кто знает, что происходит не так?
public class ListEquippedItems : MonoBehaviour
{
private static ListEquippedItems instance = null;
public static ListEquippedItems Instance
{
get
{
if (instance == null) instance = new ListEquippedItems();
return instance;
}
}
[SerializeField]
private GameObject[] currentlyEquippedItems = new GameObject[8];
void Awake()
{
// Putting fake items into the currently equipped to test that items can actually be transfered to it
for (int i = 0; i < currentlyEquippedItems.Length; i++)
{
//////////////////// In 3 below lines the array works just fine and returns proper values.
currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().SetItemType((EffectTypes)i);
currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().SetItemName("I am item: " + i);
currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().SetItemIcon(RewardAssetContainer.Instance.commonExample);
Debug.Log(currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().GetItemIcon().ToString());
Debug.Log(currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().GetItemType().ToString());
Debug.Log(currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().GetItemName());
}
}
public void SetEquippedItem(EffectTypes _itemType, Sprite _itemIcon, string _itemName)
{
for (int i = 0; i < currentlyEquippedItems.Length; i++)
{
Debug.Log(currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().GetItemType().ToString());
Debug.Log(currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().GetItemName());
Debug.Log(currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().GetItemIcon().ToString());
//////////////////// In 3 below lines the array returns null references, but why?
if (currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().GetItemType() == _itemType)
{
currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().SetItemName(_itemName);
currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().SetItemIcon(_itemIcon);
break;
}
}
}
SetItemName
,SetItemType
иSetItemIcon
, поскольку они кажутся членами вашего классаListEquippedEntry
, и я считаю, что это может иметь некоторое влияние. На первый взгляд кажется, что ваш код должен работать нормально. - person   schedule 03.10.2017