- Home /
Damage Over Time Help?
Sorry, please have mercy on my extreme noobiness. I need help with a damage over time code, preferably not a separate script. Basically I made a hunger and thirst bar using a tutorial, because I'm nooby like that, and i need them to degrade 1 hunger point/thirst point (out of total 100 hunger/thirst points) every 10 seconds unless it's already full, but no matter how much I try myself, I can never get it to work, all i need is a tiny little code snippet. I'm so sorry for asking for a premade thing, but I'm really desperate... All I know is that it has something to do with:
"Time.deltaTime;"
and
"while(curHunger < maxHunger)
curHunger - 1"
Or something like that.
This is the code I use for my hunger bar, it was modified from BergZurgArcade's Health Bar...
using UnityEngine;
using System.Collections;
public class PlayerHunger : MonoBehaviour {
public int maxHunger = 100;
public int curHunger = 100;
public float hungerBarLength;
// Use this for initialization
void Start () {
hungerBarLength = Screen.width / 4;
}
// Update is called once per frame
void Update () {
AdjustCurrentHunger(0);
}
void OnGUI(){
GUI.Box(new Rect(5, 540, hungerBarLength, 20), curHunger + "/" + maxHunger);
}
public void AdjustCurrentHunger(int adj){
curHunger += adj;
if(curHunger < 1)
curHunger = 0;
if(curHunger > maxHunger)
curHunger = maxHunger;
if(maxHunger < 1)
maxHunger = 1;
if(curHunger <= 0)
Destroy(gameObject);
hungerBarLength = (Screen.width / 4) * (curHunger / (float)maxHunger);
}
}
BY THE WAY, I NEED THE CODE SNIPPET TO BE IN C# Thanks in advance for your co-operation!
Answer by whydoidoit · May 17, 2013 at 08:01 PM
void OnEnable()
{
InvokeRepeating("DegradeHunger", 10, 10);
}
void DegradeHunger()
{
if(curHunger<maxHunger) curHunger++;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613102441im_/https://answers.unity.com/themes/thub/images/avi.jpg)