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?

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.