I have a hunger system that makes player hungry after a certain time. But what kind script I should make for the consumables? In the hunger system -script it checks the hunger every frame (Update() function).
Just add a simple script that stores the amount of “food value” the food item has and allows access to that value. E.g. a single grape would have a small “food value”, while an entire roast turkey might have a huge “food value”.
Example (using C#):
using UnityEngine;
using System.Collections;
public class EdibleItem : MonoBehaviour {
// This is a C# Property...
private int _foodValue;
public int foodValue
{
get
{
return _foodValue;
}
set
{
_foodValue = _foodValue;
// add any necessary sanity checks here.
}
}
}
Then you just access the item’s “EdibleItem” script from the script that handles collectable items. If it sees such a script script attached to an item, it gets the value stored in the foodValue variable and adjusts the player’s hunger variable accordingly.
Thank you.
Wouldn’t this do nothing? I think you need to
_foodValue = value;