Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Healthbar issue

I'm having issues when following the 2D Platformer tutorial. Everything was working fine until I finished the WaveSpawner section. The wave spawner works but now when I die and respawn, my health bar and numbers do not reset . I also moved the health bar from being a child of the Player and docked it to the camera so it could be in the upper left corner. I don't see how moving the health bar could effect it after respawning.

If anyone has any ideas what could be causing this issue, please let me know. I'm not receiving any errors.

Here's the script I have for the health bar.

using UnityEngine;
using UnityEngine.UI;


public class StatusIndicator : MonoBehaviour
{


	[SerializeField]
	private RectTransform healthBarRect;
	[SerializeField]
	private Text healthText;


	void Start()
	{
		if (healthBarRect == null)
		{
			Debug.LogError("STATUS INDICATOR: No health bar object referenced!");
		}
		if (healthText == null)
		{
			Debug.LogError("STATUS INDICATOR: No health text object referenced!");
		}
	}


	public void SetHealth(int _cur, int _max)
	{
		float _value = (float)_cur / _max;


		healthBarRect.localScale = new Vector3(_value, healthBarRect.localScale.y, healthBarRect.localScale.z);
		healthText.text = _cur + "/" + _max + " HP";
	}


}


Best Answer

  • ComradePinguComradePingu Member
    edited July 22 Accepted Answer

    Hi!

    Inject this to your player script:

    void Start()

    {

    (rest of the Start method)

    Init() ;

    }


    public void Init()

    {

    curHealth = maxHealth;

    }


    Brackeys fixed this issue 2-3 videos later

Answers

Sign In or Register to comment.