How to restore hunger value using scriptable objects?

I have a hunger value as scriptable object:

[CreateAssetMenu]

public class PlayerHunger : ScriptableObject
{
    public float hunger = 100f;
    public float maxHunger = 100f;
    public float hungerRate;

    /*private void OnEnable()
    {
        hunger = hunger - (hungerRate * Time.deltaTime);
    }*/

    private void OnDisable()
    {
        hunger = maxHunger;
    }
}

A food item as scriptable object

public enum ItemType
{
    Food,
    Equipment,
    HouseDecor,
    Default
}

public abstract class ItemObject : ScriptableObject
{
    public GameObject prefab;
    public ItemType type;
    [TextArea(15, 20)]
    public string description;
}
using UnityEngine;

[CreateAssetMenu]
public class FoodItem : ItemObject
{
    public PlayerHunger hungerValue;
    public float restoreHunger;

    public void Interaction()
    {
        hungerValue.hunger += restoreHunger;
    }




}

How do I writ this food item script so that whenever the player moves over the mouse on it and click on it, its hunger value increase?
I assume I’d be using something like Raycast manager right?

Raycasting is one solution. There are others. You should work through a few tutorials on picking stuff up. It isn’t only code that someone can simply type out here. It’s a combination of code and in-scene asset setup.

@Kurt-Dekker Thanks so I have watched a few raycast tutorial and came up with this

public class SelectionManager : MonoBehaviour
{
    [SerializeField] private string selectableTag = "Selectable";
    [SerializeField] private Material highlightMaterial;
    [SerializeField] private Material defaultMaterial;
   
    private Transform _selection;
    private void Update()
    {

        if (_selection != null)
        {
            var _selectionRenderer = _selection.GetComponent<Renderer>();
            _selectionRenderer.material = defaultMaterial;
            _selection = null;
        }
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            var selection = hit.transform;
            if (selection.CompareTag(selectableTag))
            {
                var selectionRenderer = selection.GetComponent<Renderer>();
                if (selectionRenderer != null)
                {
                    selectionRenderer.material = highlightMaterial;
                }

              
               
                _selection = selection;
            }
           
        }
    }
}

And I use input.getmousebuttondown(0) in the food item script

using UnityEngine;

[CreateAssetMenu]
public class FoodItem : ItemObject
{
    public PlayerHunger hungerValue;
    public float restoreHunger;

    public void Eat()
    {
        if( hungerValue.hunger >= hungerValue.maxHunger)
        {
            hungerValue.hunger = hungerValue.maxHunger;
        }

      
       
       
        if (Input.GetMouseButtonDown(0))
        {
            if (hungerValue.hunger < hungerValue.maxHunger)
            { hungerValue.hunger += restoreHunger; }

        }
    }




}

The value still doesn’t restore. I feel like I might have to delete all files and start again…

When I tried to drag the foodItem script onto my food object, I get the error saying “Script’s type not found”

You cannot get all this working at one time, otherwise you’ll have multiple confounding errors as you report above.

Stand up the parts necessary in the correct order, and do not move onto the next step until it works:

  • throw some food in the scene, marked however you want

  • look at it and make sure you sense it with whatever raycasting solution (or proximity) you choose to use (prove it with Debug.Log())

  • when you sense it is near, make sure you accept user intention to “Take it” (or eat it or whatever) (prove it with Debug.Log())

  • now connect that user intention to whatever restores your food / health (prove it changed with Debug.Log())

  • now make sure whatever UI you’re using is updated.

You can never get all this working as one giant sneeze of code. You must iterate and build upon success, not upon top of other existing unknown issues. Do not move forward until everything up to that point is working. You should also use source control so if something stops working, you just go back and revert and it is all better again.