Clicking problem on 2D

Hi all, I am new in Unity. I am creating a 2D game that the enemies can be killed by a mouse click. I created Prefab and attached the enemy on it(animator), then I write the following code:

public GameObject magic;
public float spawanrate = 2;
private float nextspawantime;
    
private GameObject magicclone;
    
void Start()
{
   nextspawantime = Time.time;
}
    
void Update()
{
    if(Time.time > nextspawantime )
    {
        Vector3 pos = new Vector3(Radom.Range(-8, 8), 10, -1);
        magiclone = (GameObject) Instantiate(magic, pos, Quaternion.identity);
        nextspawantime  = Time.time + spawanrate ;
    }
    
}

Also, I added HitOnEnemy for killing the enemy.

public int mydamage;

void Start()
{
}

void Update()
{
   if (Input.GetMouseButtonDown(0))
   {
      Vector2 pos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
      RaycastHit2D hitInfo = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(pos), Vector2.zero);
      if (hitInfo.collider != null)
      {
         if (hitInfo.transform.gameObject.tag == "Magic")
         {
           Debug.Log("hit");
           MagicState mc = (MagicState)this.GetComponent(typeof(MagicState));
           mc.ApplyDamage(mydamage);
         }
      }
   }


}

When I run the game, I got a strange situation is that I click on the enemy (one click only), I got more than 5 “hit” on the console like this:

hit
UnityEngine.Debug:Log(Object)

hit
UnityEngine.Debug:Log(Object)

hit
UnityEngine.Debug:Log(Object)

hit
UnityEngine.Debug:Log(Object)

hit
UnityEngine.Debug:Log(Object)

Help me please, I have spent a week but could not solve this problem.

------Update-------
Thank you everyone who answered my questions. I think, I find the problem which comes from the Floor object (Quad). Because when the Quad is removed, everything is good. So that, I have a work around that I use a Sprite for the background to instead of Quad.

Try Removing the Hit code from the Update function and add it in a separate OnMouseDown function. Like so:

void OnMouseDown(){ 
      MagicState mc = (MagicState)this.GetComponent(typeof(MagicState));
      mc.ApplyDamage(mydamage);
}    

Update:
I tested your code and debugged it and tried my method. Everything worked fine with single or multiple instances of your character. THe character was taking damage properly and the Hp was being deducted once per character until he disappears. Try this updated file with a clean project. [30068-tested+on+pc.zip|30068]

if the collider is attached to the sprite then void OnMouseDown func would get called on mouse click

or

private Collider2D cols;
if( Input.GetMouseButtonDown(0)
{
Vector3 clickPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
int check = Physics2D.OverlapCircleNonAlloc( clickPosition,0.3f, cols);

if( check != 0)
{
	cols[0].SendMessage("processTap"); // the first object 
	
}

}

Thanks for your answers. However, I have tried the answers that provided, it still doesn’t work (the code inside onMouseDown doesn’t run at all).

Any idea[30066-clicking_preblem.zip|30066]s, Thanks a lot again.

I attach part of my project, I think it would be easier to understand.

I think your problem is that in your original code every frame you are checking if the mouse is down, so this would register as multiple “clicks”. Using OnMouseDown is the obvious solution but if you are saying this does not work then try adding a one-shot boolean to the code so it can’t register the click multiple times. In addition to this you should try attaching the two original scripts to an empty GameObject in your scene which deals with both spawning and click detection remotely so there is no possibility of multiple instances of the scripts running (I presume HitOnEnemy is attached to every prefab when it shouldn’t necessarily be). You will have to edit your code a little to enable this.

If the various coding answers have not achieved anything maybe it is worth looking at Unity itself and the properties of your prefabs.