Finding Player Health using script for Enemy Prefab

Hi,

So I’m creating a top down 2D roguelike and I want to spawn enemies in each room. However since this is to be randomized multiple of the same enemy types can exist at any given time. The only problem I am having is that the enemies have a “Collide” script which lowers the players health if they touch this enemy.

This of course needs a reference to the player’s health, but the prefab is unable to locate the player’s health (you can see the bold term in the collide script) and I’m unsure as to how to automate this reference.

The health is broadly a part of a script I have where I define player health, but is there a way I can simply reference the player game object itself? In this case," Player.Canvas.Healthbar.Health " or something similar?

I know I can make the enemies find their own health bar as it is a part of the enemy’s own object tree but I don’t know how to refer to another object’s tree.

Sorry for the noob question, I’m very much learning as I go here.

Thanks

Here is the code for PlayerStats in which health is defined:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerStats : MonoBehaviour
{
private Image content;

public GameObject player;



[SerializeField] private Text statValue;

[SerializeField] private float lerpSpeed;

private float currentFill;

private float currentValue;

public float MyMaxValue { get; set; }

public float MyCurrentValue
{
    get
    {
        return currentValue;
    }
    set
    {
        if (value > MyMaxValue)
        {
            currentValue = MyMaxValue;
        }
        else if (value < 0)
        {
            currentValue = 0;
        }
        else
        {
            currentValue = value;
        }

        currentFill = currentValue / MyMaxValue;

        statValue.text = currentValue + " / " + MyMaxValue;
    }

}

// Start is called before the first frame update
void Start()
{
    MyMaxValue = 100;
    content = GetComponent<Image>();

    player = GameObject.FindGameObjectWithTag("PlayerHealth");
}

// Update is called once per frame
void Update()
{
    if (currentFill != content.fillAmount)
        content.fillAmount = Mathf.Lerp(content.fillAmount, currentFill, Time.deltaTime * lerpSpeed);
    //Debug.Log(MyCurrentValue);
}

public void Initialize(float currentValue, float maxValue)
{
    MyMaxValue = maxValue;
    MyCurrentValue = currentValue;
}

}

An extension on my comment

public class PlayerStats : MonoBehaviour { 

	//Very basic Singleton - Read about thread safe ways
	public static PlayerStats Instance{get; private set;}
	
	public HealthBar healthBar; //Set this how ever you want
	
	private int _maxHealth;
	private int _health;
	public int Health {
		get{
			return _health;
		}
		set{
			_health = value;
			if(_health <= 0)
			{
				//Out of health do something;
			}
			else
			{
				healthBar.UpdateBar(_health / _maxHealth); // To send the ratio of health remaining to then set the fillamount on the health bar
			}
		}
	}
	
	void Awake()
	{
		//Set the instance to this instance of PlayerStats
		Instance = this;
	}
} 

And then you update it with

PlayerStats.Instance.Health -= Damage

I suggest that route.