Cant use OnMouseDown

Hi!

I don’t find a right title, sorry for that.

The problem is, that i have an automatic turret, wich targets enemies in range and attack.
That works very well but i will click on the enemy too, to make some damage (its a defense like game).

If the turret target on enemy i can’t click on them. The OnMouseDown function will not work.

Enemy Hitbox Script:

using UnityEngine;
using System.Collections;

public class hitbox :  MonoBehaviour
{

    public GameObject go;
    private BasicEnemy g;
    private float life = 2;


    //Start function
    public void Start()
    {
        g = (BasicEnemy)go.GetComponentInParent(typeof(BasicEnemy));
    }

    public void damage(float amount)
    {
       
        g.onHit();

        if (amount > 0)
        {
            if((life - amount) <= 0)
            {
                destroy ();
            }
            else
            {
                life = life - amount;
            }
        }
    }

    void Update()
    {
        if(life <= 0)
        {
            destroy ();
        }
    }

    public void setLife(float amount)
    {
        if(amount > 0)
        {
            life = amount;
        }
    }

    void OnMouseDown()
    {
        damage (1);
    }

    void destroy()
    {
        Destroy(go);
    }

    public float getLife()
    {
        return life;
    }

}

Turret Script:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BasicTurret : MonoBehaviour, TurretInterface
{
    public Transform gunEnd;
    protected float shootRate;
    protected float shootRange;
    public float shootDamage;
    protected float nextFire;
    protected hitbox enemy;
    protected SphereCollider myCollider;
    protected LineRenderer laserLine;
    protected Queue<hitbox> enemyQueue = new Queue<hitbox>();

    // Use this for initialization
    public void Start ()
    {
        shootRange = 4.5f;
        shootRate = 0.5f;
        shootDamage = 1;
        laserLine = GetComponent<LineRenderer>();
        myCollider = transform.GetComponent<SphereCollider> ();
        myCollider.radius = shootRange;
    }
   
    // Update is called once per frame
    public void FixedUpdate ()
    {
        if(enemy != null)
        {
            this.gameObject.transform.LookAt (enemy.transform);
        }
        else
        {
            laserLine.enabled = false;
            if (enemyQueue.Count > 0)
            {
                if (enemyQueue.Peek () != null)
                {
                    enemy = enemyQueue.Dequeue ();
                    this.gameObject.transform.LookAt (enemy.transform);
                }
            }
        }
    }
       
    protected void Update()
    {
        if (enemy != null && Time.time > nextFire)
        {
            nextFire = Time.time + shootRate;

            laserLine.SetPosition (0, gunEnd.position);

            laserLine.SetPosition (1, enemy.transform.position);

            StartCoroutine (attack ());
        }
    }
       
    public void setRange(float amount)
    {
        if(amount > 0)
        {
            shootRange = amount;
        }
    }

    public void setRate(float amount)
    {
        if(amount > 0)
        {
            shootRate = amount;
        }
    }

    public void setDamage(float amount)
    {
        if (amount > 0)
        {
            shootDamage = amount;
        }
    }
    public void OnTriggerEnter(Collider other)
    {
        if (enemy == null)
        {
            if (other.gameObject.GetComponentInParent<BasicEnemy> ())
            {
                enemy = other.gameObject.GetComponent<hitbox> ();
                this.gameObject.transform.LookAt (enemy.transform);
            }
        }
        else
        {   
            enemyQueue.Enqueue (other.gameObject.GetComponent<hitbox>());
        }
    }

    IEnumerator attack()
    {
        yield return new WaitForSeconds (0.02f);
        enemy.damage (shootDamage);
        laserLine.enabled = true;
        yield return new WaitForSeconds (0.08f);
        laserLine.enabled = false;
    }

    public void OnTriggerExit (Collider other)
    {
        if(enemy != null)
        {
            if(other.gameObject == enemy)
            {
                enemy = null;
            }
        }
    }
       
}

I hope u understand my problem xD

OnMouseDown requires a RigidBody to “catch” the clicks from a Collider, even if you’re not using physics. (Be sure to uncheck Use Gravity on the RigidBody component or your object will “fall”.) Any OnMouse script must be on the same object as the RigidBody. If you just have one Collider, that’s all it should take.

If you have multiple Colliders on a single object that could receive a click, the RigidBody must be on a parent object, not on the object itself, and you’ll need to figure out which Collider triggered the event.

For example, I have a ListBox prefab. It has child objects for ListItems, ScrollbarUp, and ScrollbarDown, all of which have Box Colliders to detect clicks. The parent ListBox object has the RigidBody and the script to detect OnMouse events. I figure out what triggered the event by storing the InstanceId of each Box Collider in the Awake. When a mouse event fires, I use a Raycast to find the Collider and compare it’s InstanceId to the list I built in Awake.

Thanks for that :smile:
The object has a RigidBody and it works fine to use the OnMouseDown until the turret gets it into the queue. The Interesting on this is, that the turret itself ignore the Enemy. Its Like its not there. But if the Enemy hits a wall it do all right and destroy it.

Looks like a bug to me and i can’t figure out where it comes from.

Are you maybe re-parenting things which could change which RigidBody receives the click event?