How to check which item I'm clicking on?

I’m making a 2D game and I’m using 3D cubes, I need to determine which cube from an 8x8 grid is being clicked on, lower it’s hp, and change the colour. This is what I have so far, but it doesn’t work and I’m not sure why.

using UnityEngine;
using System.Collections;

public class colorChange : MonoBehaviour
{

    int hp = 3;

    // Use this for initialization
    void Start()
    {
        GetComponent<Renderer>().material.color = Color.green;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonUp(0))
        {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 100))
            {
                if (hit.collider.name == "Cube")
                {
                    if (hp == 3)
                    {
                        this.GetComponent<Renderer>().material.color = Color.yellow;
                        hp--;
                    }
                    else if (hp == 2)
                    {
                        this.GetComponent<Renderer>().material.color = Color.red;
                        hp--;
                    }
                    else
                    {
                        this.GetComponent<Renderer>().material.color = Color.black;
                        hp--;
                    }
                }
            }
        }
    }
}

Well, because you are casting a ray that can affect any object called cube, then lowering the ‘hp’ of the object the script is attached to rather than that object, it isn’t going to work in the way you intend.

 using UnityEngine;
 using System.Collections;
 
 public class colorChange : MonoBehaviour
 {
 
     int hp = 3;
 
     // Use this for initialization
     void Start()
     {
         GetComponent<Renderer>().material.color = Color.green;
     }
 
     // Update is called once per frame
     void Update()
     {
                     if (hp == 3)
                     {
                         this.GetComponent<Renderer>().material.color = Color.yellow;
                     }
                     else if (hp == 2)
                     {
                         this.GetComponent<Renderer>().material.color = Color.red;
                     }
                     else
                     {
                         this.GetComponent<Renderer>().material.color = Color.black;
                     }
     }
 }

Attach the above to your cubes, then the following to your camera;

 using UnityEngine;
 using System.Collections;
 
 public class colorChangeCamera : MonoBehaviour
 {
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetMouseButtonUp(0))
         {
             var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit, 100))
             {
                 if (hit.collider.name == "Cube")
                 {
                     hit.transform.GetComponent.<colorChange>().hp--;
                 }
             }
         }
     }
 }