- Home /
How to fix monobehavour-pickups together with class-copying
My problem is the following: I have a couple of classes(pots and other items) that inherit from my class items and override the use-function. This classes are not from monobehavour in order to be able to copy them with my copyconstructor. Now i want to make a script to make pickup-items in the scence, that adds the items to some inventory. How can i save the different classes flexible enough so i can still call the operwritten use(). The only idea I had was to make a Object-Array to save the classes leaving me unable to get the classes back once i saved them as object. I would be rly happy if someone would have some general idea how to work around this problem. I feel like i miss some basic knowledge... Anyway this are some of the more important scripts: a bit long but should not go into depth too much at all; thanks for your help guys :)
using System; using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Item {
string _name = "Demoname";
public Player player;
int _vkpreis = 1;
int _kaufpreis = 2;
int _maxstack = 1;//wieoft stackbar
int _count = 1;
private bool _onplayer = false;
public Item(Item lastitem)
{
player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
_name = lastitem.Itemname;
_vkpreis = lastitem.Vkpreis;
_kaufpreis = lastitem.Kaufpreis;
_maxstack = lastitem.Maxstack;
_count = lastitem.Count;
_onplayer = lastitem.Onplayer;
}
public Item(bool onplayer)
{//assign values
}
public Item()
{//assign values
}
public Item(string itemname, int vkpreis, int kaufpreis, int maxstack, int count,bool onplayer)
{
}
public virtual void Setup(string itemname, int vkpreis, int kaufpreis, int maxstack, int count,bool onplayer)
{
player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
_name = itemname;
_vkpreis = vkpreis;
_kaufpreis = kaufpreis;
_maxstack = maxstack;
_count = count;
_onplayer = onplayer;
}
public void Setup(Item item)
{
player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
_name = item.Itemname;
_vkpreis = item.Vkpreis;
_kaufpreis = item.Kaufpreis;
_maxstack = item.Maxstack;
_count = item.Count;
_onplayer = item.Onplayer;
}
public int Count
{
get { return _count; }
set {
if (value > _maxstack)
{
_count = _maxstack;
}
else if (value < 0)
{
_count = 0;
}
else
{
_count = value;
}
}
}
public virtual void Use() { }
public int Maxstack
{
get { return _maxstack; }
set { _maxstack = value; }
}
public int Kaufpreis
{
get { return _kaufpreis; }
set { _kaufpreis = value; }
}
public int Vkpreis
{
get { return _vkpreis; }
set { _vkpreis = value; }
}
public string Itemname
{
get { return _name; }
set { _name = value; }
}
public bool Onplayer
{
get { return _onplayer; }
set { _onplayer = value; }
}
}
Then this is my HealPotion-Script for example
public class LifePotion : Item
public int healsize = 0
public void Setup(string itemname, int vkpreis, int kaufpreis, int maxstack, int count,int healSize, bool onplayer)
{
Itemname = itemname;
Vkpreis = vkpreis;
Kaufpreis = kaufpreis;
Maxstack = maxstack;
Count = count;
healsize = healSize;
Onplayer = onplayer;
}
public override void Use()
{
if (Count > 0 && (player.curHealth < player.maxHealth))
{
player.Heal(healsize);
Count = Count - 1;
}
}
public void Start()
{
player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
Itemname = "Redpot";
Maxstack = 2;
Count = 1;
Vkpreis = 1;
Kaufpreis = 2;
healsize = 10;
}
}
and my inventory script, i saved them as items for now:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Itemhandler : MonoBehaviour {
public Item[] items = new Item[0];
private void Start()
{
}
public bool AddItem(Item item)
{//returnt ob aufgehoben wird
try
{
for (int i = 0; i < items.Length; i++)
{
if (item.Itemname == items[i].Itemname)
{
if (items[i].Maxstack == items[i].Count)
{
items[i].Count = items[i].Count + item.Count;
return false;
}
else
{
// Debug.Log(items[i].Count.ToString() + item.Count.ToString() + item.Maxstack.ToString() + items[i].Maxstack.ToString() + item.Vkpreis.ToString());
items[i].Count = items[i].Count + item.Count;
return true;
}
}
}
Resize(items.Length + 1, ref items);//???buggy when items emty, maybe try/catch useless lateron
items[items.Length - 1] = new Item(item);
Debug.Log("itemlen:" + items.Length);
return true;
}
catch (Exception err)
{
//nothing
Debug.Log("itemlen:" + items.Length);
Debug.Log(err.Message);
Resize(1, ref items);
items[items.Length - 1] = new Item(item);
return true;
}
}
private void Resize(int Size, ref Item[] itemss)
{
Item[] temp = new Item[Size];
for (int c = 1; c < Mathf.Min(Size, itemss.Length); c++)
{
temp[c] = new Item(itemss[c]);
}
itemss = temp;
}
public int Get_indexByName(string _name)
{
for (int i = 0; i < items.Length; i++)
{
if(items[i].Itemname == _name)
{
return i;
}
}
return -1;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612111338im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Collecting items when you destroy something? 1 Answer
pickup items please help 4 Answers
Adding scripts to items in a list 1 Answer
item pick up menu 1 Answer
Using a non-monobehaviour class file 1 Answer