Witam. Dzisiaj próbowałem zrobić sobie ekwipunek do gry w Unity3D. Taki do testów. Niestety podczas wejścia w trigger obiektu, który chcę podnieść, skrypt zwraca błąd.
Link do screenshota z errorem: https://imgur.com/QxF6VC4
Poniżej wrzucam wszystkie skrypty, które zostały wykorzystane do tworzenia tego ekwipunku.
1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EatItem : ItemAbstract {
public int regenerateHP = 20;
public override bool execute(PlayerStats ps)
{
ps.playerHP = (ps.playerHP + regenerateHP);
return disposable;
}
}
2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class ItemAbstract : MonoBehaviour {
[Header("Zmienne do inwentarza")]
public Texture2D itemIcon;
public string itemName;
public string description;
public string type;
public float weight;
public float cost;
public bool disposable;
public Texture2D getItemIcon()
{
return itemIcon;
}
public string getName()
{
return itemName;
}
void OnTriggerEnter(Collider other)
{
if(other.tag.Equals("Player"))
{
other.GetComponent<Equipment>().SendMessage("addItem", this);
Destroy(gameObject);
}
}
public abstract bool execute(PlayerStats ps);
}
3
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Equipment : MonoBehaviour
{
public int itemsInRow = 3;
public int buttonWidth = 50;
public int buttonHeight = 50;
public int buttonMargin = 10;
public Rect inventoryPosition = new Rect(20, 20, 0, 0);
private List<ItemAbstract> itemsList = new List<ItemAbstract>();
private bool openInventory;
private PlayerStats ps;
void Start()
{
openInventory = false;
ps = gameObject.GetComponent<PlayerStats>();
inventoryPosition = new Rect(inventoryPosition.x, inventoryPosition.y, ((itemsInRow * buttonWidth) + ((itemsInRow + 1) * buttonMargin)), ((itemsInRow * buttonHeight) + ((itemsInRow + 1) * buttonMargin)));
}
void Update()
{
if (Input.GetKeyDown(KeyCode.I))
{
openInventory = !openInventory;
}
}
public void addItem(ItemAbstract item)
{
itemsList.Add(item);
}
void OnGUI()
{
if (openInventory)
{
int numerOfItems = itemsList.Count;
int rows = Mathf.CeilToInt(numerOfItems / itemsInRow) + 1;
GUILayout.BeginArea(inventoryPosition);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < itemsInRow; j++)
{
if ((i * itemsInRow) + j < numerOfItems)
{
if (GUI.Button(new Rect((buttonMargin + buttonMargin * j + buttonWidth * j),
(buttonMargin + buttonMargin * i + buttonHeight * i),
buttonWidth,
buttonHeight), itemsList[(i * itemsInRow) + j].getItemIcon()))
{
bool disposable = itemsList[(i * itemsInRow) + j].execute(ps);
if (disposable)
{
itemsList.RemoveAt((i * itemsInRow) + j);
}
}
}
else
{
if (GUI.Button(new Rect((buttonMargin + buttonMargin * j + buttonWidth * j), (buttonMargin + buttonMargin * i + buttonHeight * i), buttonWidth, buttonHeight), ""))
{
Debug.Log("Clicked the button!");
}
}
}
GUILayout.EndArea();
}
}
}
}
Co może być nie tak?