OnMouseDown with Tags

Hello all,

I want to be able to click on any gameobject, and if it is tagged with “ClickArea”, an int increases. I have a script, but the click does not register:

-the gameobjects being clicked are tagged
-my script is attached in scene
-debug.log does not return.

While researching, I found this similar answer: OnMouseDown issues!! - Questions & Answers - Unity Discussions

@getyour411 , you mentioned that you thought you could take a look at this other thread. Does anyone know why this is happening?

my script:

using UnityEngine;
using System.Collections;

public class playerpref1 : MonoBehaviour {


	public CarrotCount _carrotcount;
	public int picked;
	// Use this for initialization
	void Start ()
	{
		picked = PlayerPrefs.GetInt ("picked");

		StartCoroutine (c1 ());

	}
	

	void OnMouseDown () 
	{
		if (gameObject.tag == "ClickArea")
		{
			picked += 1;
			Debug.Log ("tag");
		}

	}

	IEnumerator c1 ()
	{
		yield return new WaitForSeconds (1);
		PlayerPrefs.SetInt ("picked", picked);
		Debug.Log (picked);
		PlayerPrefs.Save ();

		StartCoroutine (c1 ());
	}
}

Any ideas?
Once again, thanks for the help.

Since you want this script on a different GameObject to what is clicked you need to use raycasts instead of the OnMouseDown() method. Here is you code using raycast:

using UnityEngine;
using System.Collections;

public class playerpref1 : MonoBehaviour
{

    public int picked;
    // Use this for initialization
    void Start()
    {
        picked = PlayerPrefs.GetInt("picked");

        StartCoroutine(c1());

    }


    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            if (Physics.Raycast(ray, out hitInfo))
            {
                if (hitInfo.collider.gameObject.tag == "ClickArea")
                {
                    Debug.Log("tag");
                    picked++;
                }
            }
        }
    }

    IEnumerator c1()
    {
        yield return new WaitForSeconds(1);
        PlayerPrefs.SetInt("picked", picked);
        Debug.Log(picked);
        PlayerPrefs.Save();

        StartCoroutine(c1());
    }
}