我在场景中有一个 3D 对象,我想通过按“E”键将其收集到库存中。但是当我添加以下行
InManag.AddItem(hit.collider.gameObject.GetComponent<Item>().item, hit.collider.gameObject.GetComponent<Item>().amount);
之前
Destroy(hit.collider.gameObject)
它停止从现场移除并且库存槽也是空的。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Item : MonoBehaviour
{
public ItemScpObj item;
public int amount;
}
if (Physics.Raycast(cam_ray, out hit, 3f, Mask_Food))
{
GUI_check = true;
GUI_text = "Press E to eat.";
if (Input.GetKeyDown(KeyCode.E) && HP < 100)
{
if (hit.collider.gameObject.GetComponent<Item>() != null)
{
InManag.AddItem(hit.collider.gameObject.GetComponent<Item>().item, hit.collider.gameObject.GetComponent<Item>().amount);
Destroy(hit.collider.gameObject);
if (HP < 100 && HP > 90)
{
float deltaHP = 100 - HP;
HP = HP + deltaHP;
}
else
{
HP = HP + 7;
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
public class InventorySlot : MonoBehaviour
{
public ItemScpObj item;
public int amount;
public bool isEmpty = true;
public GameObject iconGo;
public TMP_Text ItemAmountText;
private void Awake()
{
iconGo = transform.GetChild(0).gameObject;
ItemAmountText = transform.GetChild(1).GetComponent<TMP_Text>();
}
public void SetIcon(Sprite icon)
{
iconGo.GetComponent<Image>().color = new Color(1,1,1,1);
iconGo.GetComponent<Image>().sprite = icon;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InventoryManager : MonoBehaviour
{
public GameObject InvPanel;
public Transform InventoryPanel;
public List<InventorySlot> invSlots = new List<InventorySlot>();
public bool isOpened;
private void Awake()
{
InvPanel.SetActive(true);
}
void Start()
{
for (int i = 0; i < InventoryPanel.childCount; i++)
{
if (InventoryPanel.GetChild(i).GetComponent<InventorySlot>() != null)
{
invSlots.Add(InventoryPanel.GetChild(i).GetComponent<InventorySlot>());
}
}
InvPanel.SetActive(false);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.I))
{
isOpened = !isOpened;
if (isOpened)
{
InvPanel.SetActive(true);
}
else
{
InvPanel.SetActive(false);
}
}
}
public void AddItem(ItemScpObj _item, int _amount)
{
foreach (InventorySlot Islot in invSlots)
{
if(Islot.item == _item)
{
Islot.amount += _amount;
Islot.SetIcon(_item.Icon);
Islot.ItemAmountText.text = Islot.amount.ToString();
return;
}
}
foreach (InventorySlot Islot in invSlots)
{
if(Islot.isEmpty == false)
{
Islot.item = _item;
Islot.amount = _amount;
Islot.isEmpty = false;
Islot.SetIcon(_item.Icon);
Islot.ItemAmountText.text = _amount.ToString();
break;
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "Food Item", menuName = "Inventory/Items/New Food Item")]
public class FoodItem : ItemScpObj
{
public float healAmount;
public void Start()
{
itemType = ItemType.Food;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum ItemType {Default, Food, Weapon, Potion }
public class ItemScpObj : ScriptableObject
{
public string itemName;
public int maximumAmount;
public GameObject itemPrefab;
public Sprite Icon;
public ItemType itemType;
public string itemDescription;
}
我已经尝试过替换对象预制件或在刚体组件中搜索问题(如果它可以被另一个对象阻挡)。