I'm trying to make a potion for my health system but I don't know how to access public properties from another code. When I though I had it, the game stop updating my Health.

My Health code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class HeartSystemNolimit : MonoBehaviour
{
public GameObject hearts;
private int life;
private int maxLife;
private bool dead;

private void Start()
{
life = hearts.Length;
maxLife = life;
}

void Update()
{
    if (dead == true)
    {
        // set gameover scene or reload level
        SceneManager.LoadScene(SceneManager.GetActiveScene().name); //reload level code
    }
}
public void TakeDamage(int d)
{
    life -= d;
    //Destroy (hearts[life].gameObject);
    hearts[life].gameObject.SetActive(false);
    if(life < 1)
    {
        dead = true;
    }
}
public void AddLife()
{
    if(life < maxLife)
    {
        hearts[life].gameObject.SetActive(true);
        life += 1;
    }
  

}
void OnTriggerEnter2D(Collider2D other)

{
   
    Bullet bullet = other.GetComponent<Bullet>();

    if (bullet != null)
    {
        TakeDamage(1);
    }

   // Destroy(gameObject);

}

}

My failed potion code
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class Health : MonoBehaviour
{
public UnityEvent interact;
private void OnTriggerEnter2D(Collider2D other)

{
    interact.Invoke();
    Destroy(gameObject);
}
//Destroy(gameObject);

}

Check if “other” contains the component first;
if(other.GetComponent()) {
//code
}

Why can you just call AddLife in the collision event? Something like :

public void OnTriggerEnter2D(Collider2D other)
{
     if (other.transform.tag == "Player")
     {
         HeartSystemNolimit Health = other.GetComponent<HeartSystemNolimit>();
         Health.AddLife();
     }
}

NOTE : Your player tag has to be “Player” for this to work…