Instantiating Prefabs on Specific Game Objects

I want users to click on specific game objects and build prefabs at the original game object’s location they just clicked on. I wrote this script to instantiate the prefab, and attached the script to each of those game objects. The problem is, when I click on any game object with that script–every game object with that script attached instantiates a cube, instead of just one at that specific location I selected. Am I missing something?

[SerializeField] private GameObject cubePrefab;
void Update()
    //if user clicks on the interactable game object
private void Spawn()
    Instantiate(cubePrefab, transform.position, transform.rotation);

Thank you so much. I’ve gone through a ton of Prefab Instantiation posts and videos, and I’m still not getting what the problem is. I feel like I’m missing some important concept here!

I used my RaycastHit to access the Transform of the interactable game object I’m selecting (using GetComponent). I then set the game objects transform to a new variable and assign that variable to my Spawn method.

Transform cubeTransform = hitInfo.collider.gameObject.GetComponent<Transform>();


Cubes now spawn one at a time only on those specific game objects when selected. Such a simple concept utter stumped me!