[SOLVED]Health gain script not working[SOLVED]

Can anyone please help why my script is not working. I am a beginner. And i need some code that prevents player to not exceed its maximum health.

public class HealthGain : MonoBehaviour {

public int healthGain = 200;
void OnCollisionEnter(Collision col){
	if (col.gameObject.tag == "Player") {
		PlayerHealth playerHealth = GetComponent<PlayerHealth> ();
		if (playerHealth != null){
			playerHealth.AddHealth(healthGain);
			Debug.Log("Health pack picked up destryoing game object");
			Destroy(this.gameObject);
		}
		
		// Assuming if there is a collision occur, and player's health is zero
		// Destroy the player gameobject
		if(playerHealth != null && playerHealth.currentHealth == 0.0f)
		{
			Debug.Log("There is no PlayerHealth component attached to this game object");
			Destroy(this.gameObject);
		}
	}
}

PlayerHealth Script

public class PlayerHealth : MonoBehaviour {
public void AddHealth(int health){
if (currentHealth + health > startingHealth){
currentHealth = startingHealth;
}
else {
currentHealth += health;
}
}
}

You need to declare your maxHealth variable and modify the AddHealth method a little.

int maxHealth = 1000;

public void AddHealth(int health){
    if (currentHealth + health > maxHealth){
        currentHealth = maxHealth;
    }
    else {
        currentHealth += health;
    }

}

You can try using Mathf.Min function. Also, you need to define your max allowed health.

public int maxHealth = yourValue; // Or Set it from inspector
int currentHealth;
public int healthGain = 200;

public void AddHealth(int health){
     currentHealth = Mathf.Max(Mathf.Min(currentHealth + health, maxHealth), 0);
}

void OnCollisionEnter(Collision col){
  if (col.gameObject.tag == "Player") {
      PlayerHealth playerHealth = GetComponent<PlayerHealth> ();
      if (playerHealth != null){
          playerHealth.AddHealth(healthGain);
      }
      Debug.Log("Destroying game object");
      Destroy(gameObject);
  }
}

void Update() {
if(health > maxHealth) {
health = maxHealth
}
}

Not sure if this is what you mean, but if it is this should help.

SOLVED

I finally solved the problem.
For those who need help for such problem.
I attached the script to player and instead of OnCollisionEnter I used OnTriggerEnter and gave a trigger box collider to the healthpack object.