change multiple texture of a gameobject on every mouse click in C#

Hello,

I am making a puzzle based game in which i have to change multiple texture of cube in every mouse click. i wrote a script and attached to every cube and able to change multiple texture of a cube, but when i click mouse anywhere in the scene all cube texture gets change at a time. I tried to handle RayCast but didnot work. Please help.

My expectation:
Change multiple texture of cube when i click on same cube.

What below code does:
Changes multiple texture of cube when i click anywhere on the scene.

using UnityEngine;
using System.Collections;

public class Raycast : MonoBehaviour 
{
	public Ray ray;
	public RaycastHit hit;
	public Texture2D[] diffuseTextures;
	public int indexPointer = 0;
	
	void Update()
	{
		ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		if(Physics.Raycast(ray, out hit))
		{

			if(Input.GetMouseButtonDown(0)){
				
				renderer.material.SetTexture ("_MainTex", diffuseTextures [indexPointer]);
				indexPointer++;
				if (indexPointer >= diffuseTextures.Length) {
					indexPointer = 0;
				}
			}
			
		}
	}
}

So you could do:

using UnityEngine;
 using System.Collections;
 
 public class Raycast : MonoBehaviour 
 {
     public Ray ray;
     public RaycastHit hit;
     public Texture2D[] diffuseTextures;
     public int indexPointer = 0;
     
     void Update()
     {
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if(Physics.Raycast(ray, out hit))
         {
 
             if(Input.GetMouseButtonDown(0)){
                 if(hit.transform.tag == "CubeTag"){
                      hit.gameObject.renderer.material.SetTexture ("_MainTex", diffuseTextures [indexPointer]);
                      indexPointer++;
                      if (indexPointer >= diffuseTextures.Length) {
                          indexPointer = 0;
                      }
                 }
             }
             
         }
     }
 }

But this would lead to the issue that the cubes change texture based on the global difuseTextures index (if that makes sense).