Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by grfdhfrgtj · Feb 19, 2017 at 10:48 PM · monobehaviourpickupitems

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;
  }
 

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges