Я пытался немного изменить свой код и разместил несколько отладочных программ, но так и не смог понять, что делаю. неправильно, насколько я понял, в инспекторе должно чего-то не хватать, но я не могу понять, чего именно.
Это ошибка:
NullReferenceException: Object ссылка не установлена на экземпляр объекта
PlayerInventory.UpdateItemDisplay () (в Assets/Scripts/Player/PlayerInventory.cs:40)
PlayerInventory.pickUpItem () (в Assets/Scripts/Player/PlayerInventory .cs:60)
PlayerInventory.Update() (в Assets/Scripts/Player/PlayerInventory.cs:25)
Здесь следует передавать данные когда я беру объект:

Вот код для инвентаря игрока:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerInventory : MonoBehaviour
{
public ItemSO itemData; // take the information of the item
private Item nearbyItem;
[Header("Keybinds")]
public KeyCode pickUpKey = KeyCode.E;
public KeyCode throwKey = KeyCode.G;
[Header("UI References")]
public Text itemNameText; // Reference to the UI Text component
public Image itemIconImage; // Reference to the UI Image component
private void Update()
{
if (Input.GetKey(pickUpKey))
{
pickUpItem();
}
if (Input.GetKey(throwKey))
{
ThrowItem();
}
}
public void UpdateItemDisplay()
{
if(itemData != null)
{
itemNameText.text = itemData.itemName;
itemIconImage.sprite = itemData.icon;
}
else
{
itemNameText.text = "";
itemIconImage.sprite = null;
}
}
private void pickUpItem()
{
if (nearbyItem != null)
{
if (nearbyItem.itemData != null)
{
itemData = nearbyItem.itemData;
Debug.Log("Picked up: " + itemData.itemName);
UpdateItemDisplay();
Destroy(nearbyItem.gameObject); // Destroy the item GameObject
nearbyItem = null;
}
else
{
Debug.LogWarning("Nearby item does not have valid item data.");
}
}
else
{
Debug.LogWarning("No nearby item to pick up.");
}
}
private void ThrowItem()
{
if (itemData != null)
{
Debug.Log("Throwing: " + itemData.itemName);
itemData = null;// delete item information
//generate object on itemholder.
UpdateItemDisplay();
}
}
public void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Item"))
{
Debug.Log("item detected " + other.name);
nearbyItem = other.GetComponent(); ; // instead save the data in the temporar container
}
}
Мой объект сценария элемента имеет только это:
public string itemName;
public GameObject itemPrefab;
public Sprite icon;
Вот как созданный мной элемент выглядит в Инспекторе:
[img]https:/ /i.sstatic.net/lYVzLd9F.jpg[/img]
Наконец, для моего предмета у меня есть только это:
public ItemSO itemData;
Подробнее здесь: https://stackoverflow.com/questions/791 ... n-instance