- Home /
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: http://answers.unity3d.com/questions/1028472/onmousedown-issues.html
@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.
The code works for me, "tag" shows up in log and picked is counting up. Are you sure the object has a collider and is not obstructed? Try it with a unity primitive.
@ASPePeX also, this script is attached to a empty gameobject, but I want to be able to click, like any of 30 other gameobjects on screen. Am I doing this the right way?
Okay this is different then. Yeah, this works only for the object it is attached to.
A raycast is what you want, something like this:
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
Debug.Log(hitInfo.collider.name);
}
}
I think that may be my problem. This script is attached to an empty gameobject, but I want to click any gameobject with the areaclick tag on-screen. How would I do this?
By the way, thanks for the help so far
would this be what I want to do? http://answers.unity3d.com/questions/615771/how-to-check-if-click-mouse-on-object.html
Answer by ASPePeX · Feb 09, 2017 at 02:08 PM
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());
}
}
Is there a way to do this with 2D sprites instead of a 3D object?
Your answer
Follow this Question
Related Questions
PlayerPrefs setting to an existing int 0 Answers
problem with playerprefs 1 Answer
Set int to object from list? 0 Answers
OnMouseDown on objects with tags 3 Answers
Creating a Playerpref score system 1 Answer