• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Wolfshadow · Feb 09, 2017 at 01:36 PM · playerprefstagsintselectiononmousedown

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.

W$$anonymous$$le researc$$anonymous$$ng, I found t$$anonymous$$s similar answer: http://answers.unity3d.com/questions/1028472/onmousedown-issues.html

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

my script:

 using UnityEngine;
 using System.Collections;
 
 public class playerpref1 : MonoBehaviour {
 
 
     public CarrotCount _carrotcount;
     public int picked;
     // Use t$$anonymous$$s 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.

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ASPePeX · Feb 09, 2017 at 01:52 PM 1
Share

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.

avatar image Wolfshadow · Feb 09, 2017 at 01:55 PM 0
Share

what does "obstructed" mean? sorry

avatar image Wolfshadow · Feb 09, 2017 at 01:56 PM 0
Share

@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?

avatar image ASPePeX Wolfshadow · Feb 09, 2017 at 02:01 PM 1
Share

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.GetMouseButtonDown(0))
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hitInfo;
             if (Physics.Raycast(ray, out hitInfo))
             {
                 Debug.Log(hitInfo.collider.name);
             }
         }
avatar image Wolfshadow ASPePeX · Feb 09, 2017 at 02:10 PM 0
Share

Wow, thanks!

Show more comments
avatar image Wolfshadow · Feb 09, 2017 at 01:59 PM 0
Share

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

avatar image Wolfshadow Wolfshadow · Feb 09, 2017 at 02:01 PM 0
Share

would this be what I want to do? http://answers.unity3d.com/questions/615771/how-to-check-if-click-mouse-on-object.html

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by ASPePeX · Feb 09, 2017 at 02:08 PM

Since you want t$$anonymous$$s 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 t$$anonymous$$s for initialization
     void Start()
     {
         picked = PlayerPrefs.GetInt("picked");
 
         StartCoroutine(c1());
 
     }
 
 
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit $$anonymous$$tInfo;
             if (Physics.Raycast(ray, out $$anonymous$$tInfo))
             {
                 if ($$anonymous$$tInfo.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());
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kaancetinkayasf · Mar 17, 2020 at 06:36 PM 0
Share

Thank you mate

avatar image AlejandroBoss10 · Aug 25, 2020 at 08:34 AM 0
Share

Is there a way to do this with 2D sprites instead of a 3D object?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

67 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Creating a Playerpref score system 1 Answer

problem with playerprefs 1 Answer

what's the result of PlayerPrefs.Get... if the key/value was never set? 1 Answer

character selection across scenes 1 Answer

How to tell if an int is even 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges