How to change value of another gameobject through script

From the picture I upload, I tried to create a game control by mouse. When I click on the red box, its HP will decrease. I want to know how to change the value of ONLY red box that I click. I tried to control with scipt but it was decrease all of the red box.

this is the code attach to purple box.

public class HPbarScript : MonoBehaviour {

    public Camera cam;
    public float MaxHealth = 100f;
    public float CurrentHealth = 0f;
    public float PercentHealth;
    public GameObject healthBar;
    public GameObject healthCanvas;
    
    public bool clickCooldown = false;
	
	void Start ()
    {
        CurrentHealth = MaxHealth;
        
    }
	
	
	void Update ()
    {
        healthCanvas.transform.LookAt(healthCanvas.transform.position + cam.transform.rotation * Vector3.back, cam.transform.rotation * Vector3.down);
        if(Input.GetMouseButtonDown(0))
        {
            if(clickCooldown == false)
            {
                CalculateHP();
                clickCooldown = true;
                Invoke("Example", 1.0f);
            }
        }        
    }
    
    void Example()
    {
        clickCooldown = false;
    }

    void CalculateHP()
    {
        
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitData;
        
        if (Physics.Raycast(ray, out hitData))
        {
            if(hitData.collider.gameObject.tag == "NPC")
            {
                CurrentHealth -= 10f;
                PercentHealth = CurrentHealth / MaxHealth;
                healthBar.transform.localScale = new Vector3(Mathf.Clamp(PercentHealth,0f,1.0f), healthBar.transform.localScale.y, healthBar.transform.localScale.z);
                
            }
            else if(hitData.collider.gameObject.tag == "Enemy")
            {
                
            }
        }
   }

}

sorry for my bad language.

Hello, Kun No Name.

Problem:

Each instances of the HPbarScript component are decreasing its own CurrentHealth variable when an enemy object is clicked.

Solution:

Get the HPbarScript component from the game object of hitData.collider. Then decrease the CurrentHealth variable of the component.

else if( hitData.collider.gameObject.tag == "Enemy" ) {
    hitData.collider.gameObject.GetComponent< HPbarScript >().CurrentHealth -= 10f;
}

Tip:

One of the best practices is to use the least amount of rays to get the job done. For every HPBarScript component in your scene, Physics.Raycast is being called when the left mouse button is clicked. That is inefficient.

A more efficient approach is to create a separate script to handle the job - using a single ray.
Here is an example:

using UnityEngine;
using System.Collections;

public class QA : MonoBehaviour
{
    void Update()
    {
        if( Input.GetMouseButtonDown( 0 ) ) {
            Attack();
        }
    }
    
    void Attack()
    {
        Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
        RaycastHit raycastHit;

        if( Physics.Raycast( ray, out raycastHit ) )
        {
            if( raycastHit.collider.gameObject.tag == "Enemy" )
            {
                raycastHit.collider.gameObject.GetComponent< HPbarScript >().CurrentHealth -= 5f;
            }
        }
    }
}

Attach that script to an object that has only one instance. You can also create an empty game object and attach the script to it.

You can also make the CalculateHP function static, but we will need to make some small changes. Example:

void ReduceHP( float damage ) {
    CurrentHealth -= damage;
    PercentHealth = CurrentHealth / MaxHealth;
    healthBar.transform.localScale = new Vector3(
        Mathf.Clamp( PercentHealth,
                     0f,
                     1.0f ),
        healthBar.transform.localScale.y,
        healthBar.transform.localScale.z );
}

static void CalculateHP()
{
    Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
    RaycastHit hitData;

    if( Physics.Raycast( ray, out hitData ) )
    {
	    float amount;
        switch( hitData.collider.gameObject.tag ) {
            case "NPC":    amount = 5f;    break;
            case "Enemy":  amount = 10f;   break;
            default:       amount = 0f;    break;
        }
	
	    if( amount > 0 )
        { hitData.collider.gameObject.GetComponent< HPbarScript >().ReduceHP( amount ); }
    }
}

This is your health bar script. It’s attached to all 4 prefabs.
When you click, all 4 health bar scripts do the same check: if clicked on tag ‘NPC’, reduce health of this health bar (‘this’ meaning the health bar doing the check == all health bars)

You need to either separate the clicking code and tag-checking to a separate script that then tells only the hit cube’s healthbar reduces health. Or just make your current script check whether it is attached to the cube that got clicked and only then reduce health.

Just be careful. hitData.collider.gameObject is the clicked cube, but you need to find its healthbar script from its children since that’s where your health is stored.