Tag calling is calling all my objects with the same tag

Hello, so i have some gameobjects (Lamps) who have the same tag name (Lights), and also i have a function that when i click on them, it turns off the component “Light” of those gameobjects, the thing is that when i click on one, all the lights are turned off in every lamp.

How can i call only 1gameobject the one im clicking to run the function to turnoff the light?
*Also i want to execute this function only if my player is standing next to the gameobject im clicking on.

Thanks!

using System.Collections;

public class StreetLampFunction : MonoBehaviour {

private Light myLight;
void Start ()
{
	myLight = GetComponent<Light>();
}

void Update()
{
	if (Input.GetMouseButtonDown(1))
	{

		RaycastHit hitInfo = new RaycastHit();
		
		bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
		if (hit) 
		{
			if (hitInfo.transform.gameObject.tag == "Light")
				//add code comparing vector position of the player and the vector position of the raycast hit position, if its next to it, execute the next line...

				{
				myLight.enabled = !myLight.enabled;
				}

		} 
}

}
}

it’s only an idea, but you can use different names for each lamp, then you have to change the line

if (hitInfo.transform.gameObject.tag == "Light")

with this new line

if (hit.transform.name == name)

try this and tell me if it works

Should be simple enough.

Seems like every light has its own raycast check from the camera to the mouse position. Since all lights fire a raycast check when you click, if you hit any of the lamps then, obviously all lamps would return a true value and fire their functions.

What you might want to do is to add a raycast script to your camera and then change the component of the transform you hit using something like:

if(hit.transform.tag == "Light"){
     hit.transform.GetComponent("Light").enabled = true;
}

This will make sure that 1: what you hit is a light. 2: that you only modify the component of the light you actually hit. Since you are only modifying the component of the transform that the raycast actually hit.

Note, this code is probably incorrect, its just to give you the idea.