Connection between scripts problem

Opened to a new thread with updated scripts:

: Making Hunger Script & Health Script Work Together ?? - Unity Engine - Unity Discussions

isn’t the solution, but update is run every frame not every second.

so if you game is running 600fps, you subtracting 6000 health every second

ya if you want every second you would need to multiple by Time.deltaTime

The reason your Dead function isn’t triggering is because your ApplyDamage function is never called, instead you’re just deducting the health directly in your Update function.

yup this…

but I think there is also a problem with the design here. It would make more sense for the hunger script to call “ApplyDamage(hungerDamage)” and have the damage rate from being hungry kept in the hunger script.

That way the health script doesn’t need to know anything about “hungry”, “ishungry” etc. (wtf is food?? I’m a healthscript!!).

Try to keep the scripts focused on what they are doing. It’ll keep things much cleaner and you wont run into so many issues later on when tweaking or extending the functionality.

1 Like

I have changed the script completely so it uses invokeRepeating now… :slight_smile:

But now i have a problem with the functions.

it looks like this:

#pragma strict

public var MaxHealth = 100;
public var Health : float;

static var NoHunger = false;
var HungerDamage = 10;

function Start ()
{
    Health = MaxHealth;
}

function ApplyDamage (TheDamage : int)
{
    Health -= TheDamage;
   
    if(Health <= 0)
    {
        Dead();
    }
}

function OnGUI()
{
    GUI.Box(new Rect(65, 10, 50, 20), "" + Health.ToString("0"));
}

function Dead()
{
    RespawnMenuV2.playerIsDead = true;
    Debug.Log("Player Died");
}

//Respawn Full Health
function RespawnStats ()
{
    Health = MaxHealth;
}

//Hungry And Taking Damage
function LoosingHealth ()
{
    Health -= HungerDamage;
   
    if(Health<0)
    {
        CancelInvoke("LoosingHealth");
        {
            Dead();
        }
    }
}

The problem is that if i put the follow into an “Function Update ()”, it goes completly crazy and my players health goes down as soon as the game starts and not when the NoHunger is equal to true…
What kind of Function should i put the invokerepeating in?

function Update()
{
    if(NoHunger == true)
    InvokeRepeating("LoosingHealth", 1, 3);
}

bump

so what would i have to type instead

Put the InvokeRepeating(“LoosingHealth”,1,3); line in the start function.

1 Like