Артём Зиненко
Ученик
(80),
на голосовании
2 месяца назад
Вот ошибка: Assets\Scripts\InventoryManager.cs(58,117): error CS0246: The type or namespace name 'Item' could not be found (are you missing a using directive or an assembly reference?) Вот код: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class InventoryManager : MonoBehaviour { public GameObject UIPanel; public GameObject crosshair; public Transform inventoryPanel; public List<InventorySlot> slots = new List<InventorySlot>(); public bool isOpened; public float reachDistance = 3f; private Camera mainCamera; // Start is called before the first frame update private void Awake() { UIPanel.SetActive(true); } void Start() { mainCamera = Camera.main; for(int i = 0; i < inventoryPanel.childCount; i++) { if(inventoryPanel.GetChild(i).GetComponent<InventorySlot>() != null) { slots.Add(inventoryPanel.GetChild(i).GetComponent<InventorySlot>()); } } UIPanel.SetActive(false); }
// Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.I)) { isOpened = !isOpened; if (isOpened) { UIPanel.SetActive(true); crosshair.SetActive(false); } else { UIPanel.SetActive(false); crosshair.SetActive(true); } } Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); RaycastHit hit;
Assets\Scripts\InventoryManager.cs(58,117): error CS0246: The type or namespace name 'Item' could not be found (are you missing a using directive or an assembly reference?)
Вот код:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InventoryManager : MonoBehaviour
{
public GameObject UIPanel;
public GameObject crosshair;
public Transform inventoryPanel;
public List<InventorySlot> slots = new List<InventorySlot>();
public bool isOpened;
public float reachDistance = 3f;
private Camera mainCamera;
// Start is called before the first frame update
private void Awake()
{
UIPanel.SetActive(true);
}
void Start()
{
mainCamera = Camera.main;
for(int i = 0; i < inventoryPanel.childCount; i++)
{
if(inventoryPanel.GetChild(i).GetComponent<InventorySlot>() != null)
{
slots.Add(inventoryPanel.GetChild(i).GetComponent<InventorySlot>());
}
}
UIPanel.SetActive(false);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.I))
{
isOpened = !isOpened;
if (isOpened)
{
UIPanel.SetActive(true);
crosshair.SetActive(false);
}
else
{
UIPanel.SetActive(false);
crosshair.SetActive(true);
}
}
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, reachDistance))
{
if (Input.GetKeyDown(KeyCode.E))
{
if (hit.collider.gameObject.GetComponent<Item>() != null)
{
AddItem(hit.collider.gameObject.GetComponent<Item>().item, hit.collider.gameObject.GetComponent<Item>().amount);
Destroy(hit.collider.gameObject);
}
}
Debug.DrawRay(ray.origin, ray.direction*reachDistance, Color.green );
}
else
{
Debug.DrawRay(ray.origin, ray.direction * reachDistance, Color.red );
}
}
private void AddItem(ItemScriptableObject _item, int _amount)
{
foreach (InventorySlot slot in slots)
{
if (slot.item == _item)
{
slot.amount += _amount;
slot.itemAmountText.text = slot.amount.ToString();
return;
}
}
foreach (InventorySlot slot in slots)
{
if(slot.isEmpty == true)
{
slot.item = _item;
slot.amount = _amount;
slot.isEmpty = false;
slot.SetIcon(_item.icon);
slot.itemAmountText.text = _amount.ToString();
break;
}
}
}
}