Food Help with Override?

Hello, All day long i try to figer out,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Food")]
public class Food : Item {

    // Need to get my CharacterStat Script in here and get my hands of the currentHealth??

    public override void Use()
    {
        base.Use();
        //currentHealth += 15;
        RemoveFromInventory();
    }

}

I was thinking some thing like this but i cant get my hand on currentHealth in my CharacterStats script.

using UnityEngine;
using UnityEngine.UI;

public class CharacterStats : MonoBehaviour {


    // HealthBar.
    public Slider healthbar;
   


    // Player Stats.
    public int maxHealth = 100;
    public int currentHealth { get; set; }

    public Stat damage;
    public Stat armor;

    void Awake()
    {
        currentHealth = maxHealth;
        healthbar.value = CalculateHealth();
       
    }


    void Update()
    {
        if (Input.GetKeyDown(KeyCode.H))
        {
            currentHealth += 15;
        }   
    }

    public void TakeDamage (int damage)
    {
        damage -= armor.GetValue();
        damage = Mathf.Clamp(damage, 0, int.MaxValue);

        currentHealth -= damage;
        healthbar.value = CalculateHealth();
        Debug.Log(transform.name + " takes " + damage + " damage.");

        if (currentHealth <= 0)
        {
            Die();
        }
    }

    float CalculateHealth()
    {
        return currentHealth / (float)maxHealth;
    }

    public virtual void Die ()
    {
        // Die in some way
        // This meathod is meant to be overwritten

        Debug.Log(transform.name + " died.");
    }

}

Pass the character using the item as a parameter to your Item’s Use() function?

1 Like

but how i tryed get the CharacterStats in the script but noo :frowning: it dosent let me get the CurrentHealth :confused:

public void Use(CharacterCombat character)
{
    CharacterStats stats = character.GetComponent<CharacterStats>();
}

I’m not really sure what issue you’re having. Could you clarify?

In the Food Script i cant get to my current healt and Chance it, i want it to be like a health pot you know ?
like when you kill a enemy he drops like food and you get 15 hp back :slight_smile:

Why though? Just use GetComponent on the character you’ve passed in to your Use() function.

it dosent work. i cant get to the curr health ,

that kind of name dosent exist in this text

using UnityEngine;

[CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")]
public class Item : ScriptableObject {

    new public string name = "New Item";
    public Sprite icon = null;
    public bool isDefaultItem = false;

    public virtual void Use (CharacterCombat character)
    {
        // Use the item 
        // Something might happen 
       
        CharacterStats stats = character.GetComponent<CharacterStats>();

        Debug.Log("Using " + name);
    }

    public void RemoveFromInventory ()
    {
        Inventory.instance.Remove(this);
    }
}

Hmm. If “stats” in that posted code is non-null, then “stats.currentHealth” should be working.

haha your always right dude tnx :smile:DD

To be fair, I’m sure that’s what @GroZZleR was trying to tell you :slight_smile:

1 Like

it dident work, my Equipment use the same Use so it all got messed up, but then i thought why not like this, but then i get this msg,

NullReferenceException: Object reference not set to an instance of an object
Consumable.Use () (at Assets/Script/Items/Consumable.cs:16)
InventorySlot.UseItem () (at Assets/Script/Inventory/InventorySlot.cs:38)
UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:165)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58)

using UnityEngine;

[CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Consumable")]
public class Consumable : Item
{

    CharacterStats character;

    // Need to get my CharacterStat Script in here and get my hands of the currentHealth??


    public override void Use()
    {
        base.Use();

        CharacterStats stats = character.GetComponent<CharacterStats>();

        stats.currentHealth += 15;

        RemoveFromInventory();
    }
}

and no errors only when i play the game and try to Consume the in this case apple

You’re no longer passing in the argument, from which you used to get the CharacterStats.