0
\$\begingroup\$

For the fire I have this script on it.

public class HeatingZone : MonoBehaviour
{
    public float heatPerSecond = 1.0f;

    void OnTriggerStay2D(Collider2D other)
    {
        PlayerHeat heat = other.GetComponent<PlayerHeat>();
        if (heat != null)
        {
            heat.Heat(heatPerSecond * Time.fixedDeltaTime);
        }
    }
}

On the player I have this script.

public class PlayerHeat : MonoBehaviour
{
    public int maxHeat = 100;
    public float currentHeat;

    public HeatBar heatBar;

    void Start()
    {
        // all objects should spawn with full health
        currentHeat = maxHeat;
        heatBar.SetMaxHeat(maxHeat);
    }

    // method for getting healed
    public void Heat(float amount)
    {
        currentHeat += amount;
        // make sure we can't heal further than max health
        if (currentHeat > maxHeat)
        {
            currentHeat = maxHeat;
        }
    }
}

And for the heat bar I have this script.

public class HeatBar : MonoBehaviour
{
    public float UpdatedHeat;
    public Image Heat;
    public float HeatDecreasePerSecond;
    public int maxHeat = 100;
    public Slider slider;
    public Gradient gradient;
    public Image fill;

    // Start is called before the first frame update
    void Start()
    {
        HeatDecreasePerSecond = -0.5f;
    }

    // Update is called once per frame
    void Update()
    {
        UpdatedHeat += HeatDecreasePerSecond * Time.deltaTime;
        Heat.fillAmount = UpdatedHeat / maxHeat;

        if(UpdatedHeat >= maxHeat)
        {
            UpdatedHeat = maxHeat;
        }
    }

    public void SetMaxHeat(int heat)
    {
        slider.maxValue = heat;
        slider.value = heat;

        fill.color = gradient.Evaluate(1f);
    }

    public void SetHeat(int heat)
    {
        slider.value = heat;

        fill.color = gradient.Evaluate(slider.normalizedValue);
    }
}

There are no errors but it is not working. Please help.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ "not working" is just about never enough information to diagnose a problem. There are many millions of ways for something to not work, including having no apparent effect, having the wrong apparent effect, runtime or compiler errors, setting your PC on fire, etc. The more you can tell us about the specific symptoms of the problem, the better we can narrow down the cause. \$\endgroup\$
    – DMGregory
    Oct 23, 2020 at 12:37
  • 1
    \$\begingroup\$ At a glance, it looks like you're tracking two different heat values. Your bar has a heat variable that goes down, and your player heat script has a heat variable that goes up. Did you mean to keep this state information in one place, so both the increase and decrease effects take place on the same stored quantity? \$\endgroup\$
    – DMGregory
    Oct 23, 2020 at 12:40
  • \$\begingroup\$ Oh maybe I mixed up heat variables. And I meant to only have one. Thanks and by not working I mean the heat bar is still going down in the fire trigger. \$\endgroup\$
    – user144438
    Oct 23, 2020 at 12:43

1 Answer 1

0
\$\begingroup\$

The problem appears to be that your PlayerHeat component does not tell the HeatBar component when its currentHeat value changes. There appears to be a method HeatBar.SetHeat which is supposed to be used for that purpose, but that method never gets called.

I would recommend to add a call to that method in the PlayerHeat.Update method. You could put it into PlayerHeat.Heat so the bar only gets updated when the heat actually changes. But you declared PlayerHeat.currentHeat as public, which means that it might get changed in other ways. So you need to keep that possibility in mind and continuously update the health bar in Update... or make that variable private and properly encapsulate access to it. (By the way: it is generally a good idea to declare everything private by default and only make things public when you have a good reason to.)

Further, it appears that you tried to implement the logic for continuously reducing heat in HeatBar.Update. Your implementation does not actually do anything, because all it does is change a variable UpdatedHeat which then does not get used for anything. But I would not put that code there anyway, because the heat loss mechanic is not a part of the heat bar on the UI. It's a part of the player heat mechanic. So it should be part of the PlayerHeat behavior (if not a separate behavior). You might also notice that the currentHeat now changes every update, so one more reason to put the call to heatBar.SetHeat into PlayerHeat.Update.

public class PlayerHeat : MonoBehaviour
{
    public int maxHeat = 100;
    public float currentHeat;
    public float heatLossPerSecond;

    public HeatBar heatBar;

    void Start()
    {
        currentHeat = maxHeat;
        heatBar.SetMaxHeat(maxHeat);
    }

    void Update(){
        currentHeat -= heatLossPerSecond * Time.deltaTime;
        if (currentHeat <= 0) {
            currentHeat = 0;
            Debug.Log("You froze to death :(");
        }
        heatBar.SetHeat(currentHeat);
    }

    public void Heat(float amount)
    {
        currentHeat += amount;
        if (currentHeat > maxHeat) {
            currentHeat = maxHeat;
        }
    }
}
\$\endgroup\$
5
  • \$\begingroup\$ Thanks so much for the code cleanup and fix. But I am still having a problem. In the console it says this. Assets\Scripts\PlayerHeat.cs(27,25): error CS1503: Argument 1: cannot convert from 'float' to 'int' This is what is on line 27 heatBar.SetHeat(currentHeat); \$\endgroup\$
    – user144438
    Oct 23, 2020 at 13:48
  • \$\begingroup\$ @jiasjfo You should decide if you want to handle heat as type int or type float and then use the same type everywhere. (I would recommend float, so you don't get any problems with small changes getting rounded to 0 and thus not doing anything). But if you do want to convert (which I would not recommend), you could do this: heatBar.SetHeat((int)currentHeat); \$\endgroup\$
    – Philipp
    Oct 23, 2020 at 13:55
  • \$\begingroup\$ Thanks so much it works! \$\endgroup\$
    – user144438
    Oct 23, 2020 at 14:42
  • \$\begingroup\$ So if i wanted to make it so when the players heat is under 40 it changes the players speed to slower. Would i say an if statement and say set player speed or something idk. \$\endgroup\$
    – user144438
    Oct 23, 2020 at 17:40
  • \$\begingroup\$ @jiasjfo Sounds like a good approach. If you have any problems with that, please open a new question. \$\endgroup\$
    – Philipp
    Oct 23, 2020 at 18:23

You must log in to answer this question.