Я сделал инвентарь но проблема в то что предметы могу быть только в слотах а в руки их взять нельзя. Пожалуйста помогите cделать так чтоб предметы можно было взять в руки. Код Inventory.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Invertory : MonoBehaviour
{
public bool [] isFull;
public GameObject[] slots;
public GameObject inventory; // сундучёк при нажатии на который выскакивает инвентарь
private bool InventoryOn;
private void Start()
{
InventoryOn = false;
}
public void Chest()
{
if (InventoryOn == false)
{
InventoryOn = true;
inventory.SetActive(true);
}
else if (InventoryOn == true)
{
InventoryOn = false;
inventory.SetActive(false);
}
}
}
PickUp.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickUp : MonoBehaviour
{
private Invertory invertory;
public GameObject slottButton; // префаб , иконка в инвентаре
private void Start()
{
invertory = GameObject.FindGameObjectWithTag("Player").GetComponent<Invertory>();
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
for (int i = 0; i < invertory.slots.Length; i++) // проходимся по всем слотам
{
if (invertory.isFull[i] == false)
{
invertory.isFull[i] = true;
Instantiate(slottButton, invertory.slots[i].transform);
Destroy(gameObject);
break;
}
}
}
}
}
Slot.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Slot : MonoBehaviour
{
private Invertory invertory;
public int i;
private void Start()
{
invertory = GameObject.FindGameObjectWithTag("Player").GetComponent<Invertory>();
}
private void Update()
{
if (transform.childCount <= 0)
{
invertory.isFull[i] = false;
}
}
public void DropItem()
{
foreach (Transform child in transform)
{
child.GetComponent<Spawn>().SpawnDroppedItem();
GameObject.Destroy(child.gameObject);
}
}
}
Spawn.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawn : MonoBehaviour
{
public GameObject item; // обэкт который находитса на сцене (не иконка)
private Transform player;
private void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
}
public void SpawnDroppedItem()
{
Vector2 playePos = new Vector2(player.position.x + 0.2f, player.position.y - 0.1f);
Instantiate(item, playePos, Quaternion.identity);
}
}