Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Deel · Feb 10, 2014 at 07:07 PM · inventoryinheritancemethodparameters

Inventroy Problem- Using different parameters in inherited classes

I have a main class "Item" and then I have classes like food and drinking which inherit from the class Item. But i have a problem with using different voids of my healthsystem to add specific values.

Item main class

 using UnityEngine;
 using System.Collections;
 
 public class Item : MonoBehaviour {
 
     virtual public void UseItem()
     {
 
 
 
     }
 }
 



Food class

 using UnityEngine;
 using System.Collections;
 
 public class Food : Item {
     public int amount;
 
 
     override public void UseItem(HealthSystem h)
     {
             h.GetFood(amount);
     }
 
     
 
 
 
 }



class for drinks

 using UnityEngine;
 using System.Collections;
 
 public class Drink: Item {
     public int amount;
 
 
     override public void UseItem(HealthSystem h)
     {
             h.GetDrink(amount);
     }
 
     
 
 
 
 }



 using UnityEngine;
 using System.Collections;
 
 public class Tool: Item {
     public int strength;
 
 
     override public void UseItem(Motor m, int a)
     {
           Repair(m,a)
     }
 
     public void Repair(Motor m, int a)
     {
         m.condition+=a;
 
     }
 
 
 
 }
 

Is this in antoher way possible?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Gnometech · Feb 10, 2014 at 07:30 PM

The problem is that the overriding method has to have the same "signature" as the base method, i.e. the number and type of parameters need to match.

You have two options:

1) Use the method as you declared it (without parameters) and also override it without parameters. Instead store the things you need in a different variable that can be accessed from the method. For instance, if your HealthSystem is constant in the game (or does not change often), you could store it in a private variable of your Food and Drink classes and refer to it. Another possibility is to let the class "pull" the needed value from somewhere else.

2) If you definitely need parameters because they change often create a custom class (e.g. "ItemUse") with fields for all possible parameters (HealthSystem, Motor, int, etc.) and pass an instance of this class with the correct type of parameters set. This way the signature stays the same across all implementations:

 virtual public void UseItem(ItemUse use)

Which of the options you take depends on the circumstances how often parameters set and if it is feasible to pass them to the class in a different way.

Comment
Add comment · Show 5 · Share
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
avatar image Deel · Feb 10, 2014 at 07:50 PM 0
Share

How can I create these fields for all possible parameters?

avatar image Gnometech · Feb 10, 2014 at 08:03 PM 1
Share

Let's see... just as an example.

 public class ItemUse
 {
 
 public HealthSystem h;
 public $$anonymous$$otor m;
 public int x;
 // Use whatever else you need
 
 // Constructor 1
 public ItemUse (HealthSystem hs)
 {
 this.h = hs;
 }
 
 //Constructor 2
 public ItemUse($$anonymous$$otor mot, int a)
 {
 this.m = mot;
 this.x = a;
 }
 
 }

This will create the class. When you call the UseItem method for food or drink, now you pass the healthsystem directly. Ins$$anonymous$$d, you would call it like

 bottle.UseItem(new ItemUse(h));

where h is your healthsystem. Similarly for the tool you would pass a new ItemUse with the motor and int value.

avatar image Deel · Feb 11, 2014 at 08:05 PM 0
Share

Thank you, that semms to work. Last question, When the food class has a value the item class dont have. how to i acess it in my inventory(list of items) ? Deel

avatar image Gnometech · Feb 12, 2014 at 06:05 AM 1
Share

There are several ways to do this. One is to try to cast your item to food and access the value only if it works.

Like this, supposing your Item variable is just called "item":

 Food food = item as Food;
 if (food != null)
 {
   // Access value here, because "food" has the correct type
 }

The trick is that the "item as Food" statement will return the same item cast to "Food" if it is an instance of the class. Any other item that is not of class food will return null. That is why the check is necessary.

With this method you can safely loop through all your items.

If my above answer works I would appreciate if you could flag it as correct. :-) Thanks.

avatar image Deel · Feb 12, 2014 at 02:40 PM 0
Share

Works perfect, thank you very much :)

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

why cant I see more than one perameter in gameobjects 1 Answer

Calling 'AddItem' Method giving me error NullReferenceException: 0 Answers

Inventory / inhertance driving me insane - help with theory please. 1 Answer

How to ignore base class? 1 Answer

What's the "right" way to deal with inheritance and constructor parameters? 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