Raycast colliding with every instance of clone on stage

public class bombClick : MonoBehaviour {
RaycastHit myhit = new RaycastHit();
private GameObject name;
Ray myray = new Ray();

	void Update () {
		myray = Camera.main.ScreenPointToRay(Input.mousePosition);
		if (Physics.Raycast(myray, out myhit, 1000.0f)  && Input.GetMouseButtonDown(0)){
			print(myhit.collider.name);
		}

	}

}

I am using unity 2d with 3d colliders. This script is attached to an enemy object constantly being randomly spawned onto the stage. If there is only one enemy on the stage, when I click it prints enemy(clone) twice. If there are 5 enemies on the stage, when I click on the hitbox, it prints enemy(clone) 6 times. This leads me to believe this script is detecting the item I click on, then every item on stage as well. I am unsure why it is doing this and I do not know what to do next.

Just incase, I will include the script attached to the camera which constantly spawns the enemies (explosions) onto the stage.

using UnityEngine;
using System.Collections;

public class bombSpawner : MonoBehaviour {
	public GameObject bomb;
	private GameObject last;
	public float randomMaxX;
	public float randomMaxY;
	public float randomMinX;
	public float randomMinY;
	public float randomX;
	public float randomY;

	// Use this for initialization
	public void Start () {
		InvokeRepeating("Spawn", 1f, 1f);
	}
	
	// Update is called once per frame
	public void Update () {

	}

	public void Spawn(){
		Vector3  screenPos;
			screenPos.z = 9;
			screenPos.x = 0 + 20;
			screenPos.y = 0 + 20;
		Vector3  worldPos = camera.ScreenToWorldPoint(screenPos);
			randomMinX = worldPos.x;
			randomMinY = worldPos.y;
			screenPos.x = Screen.width - 20;
			screenPos.y = Screen.height - 20;
		worldPos = camera.ScreenToWorldPoint(screenPos);
			randomMaxX = worldPos.x;
			randomMaxY = worldPos.y;

		randomX = Random.Range (randomMinX,randomMaxX);
		randomY = Random.Range (randomMinY,randomMaxY);

		Vector3 randomLocation = new Vector3(randomX,randomY,9.0f);
		last = Instantiate(bomb, randomLocation, transform.rotation) as GameObject;
			Destroy(last,5.0f);

	}
	
}

Here is a link for the project: http://www.filedropper.com/explosionmaker
Open bombgame.unity to see the project.

I also can’t get:
void OnCollisionEnter(Collision Collection)

	{
		
		print("collide");
		
	}

to fire. I think the problem is because I can only attach a script to the parent object and the oncollisionenter doesnt activate for the instantiated clones. I can’t find any information on how to fix this problem though.

If anyone can help with any of my current issues I would really appreciate it! :slight_smile: Just started learning and have really hit a brick wall on this one

Clearly you want to check if the enemy instance is been clicked. However that script you write will print the log once you click on something, not a particular enemy. And since the script is instantiated several times, it’s not suprising you get the same log several times.
here’s what you should write:

public class bombClick : MonoBehaviour {
    private GameObject name;    
     
    void Update () {
        Ray myray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;
        //note: check mouse click first, it cost much less than raycast.
        if (Input.GetMouseButtonDown(0) && Physics.Raycast(myray, out hitInfo, 1000.0f)){
            if(hitInfo.collider.transform == transform)
                print(myhit.collider.name);
        }
     
    }
     
}