• 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 Prqtection · Oct 24, 2022 at 04:08 AM · raycastscripting beginneronmousedown

OnMouseDown instead of Raycasting? [Beginner Question]

So I am trying to make it so that when "Wheat" is clicked it will Debug.Log somet$$anonymous$$ng (for now). The raycast doesn't seem to be working, but the OnMouseDown is detecting it. Is there a way I can switch it to OnMouseDown, and if not, what is wrong with my Raycast code? Any help would be appreciated, and thanks! //code

private void Update() {

     if (Input.GetMouseButtonDown(0))
      {
          Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
          RaycastHit $$anonymous$$tInfo;
          if (Physics.Raycast(ray, out $$anonymous$$tInfo))
          {
             Debug.Log("smt");
              if ($$anonymous$$tInfo.collider.gameObject.tag == "Wheat")
              {
                  Debug.Log("tag");
                  
              }
          }
      }
     
 }
Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SurreyMuso · Oct 24, 2022 at 06:35 AM

OnMouseDown detects a click when over the game object with the script. If you click elsewhere, the wheat code is never called. You don't need the Ray cast since you know you have clicked on wheat to get t$$anonymous$$s far.

Comment
Add comment · Show 4 · 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 logicandchaos · Oct 24, 2022 at 01:14 PM 0
Share

Edit ya they are right, sorry about the down vote, won't let me change it now..

That's not true tho.. OnMouseDown detects any clicks. You should verify your answers before answering, spreading false information helps no one.

avatar image SurreyMuso logicandchaos · Oct 25, 2022 at 07:25 AM 1
Share

@logicandchaos I'm not sure what you're saying here. OnMouseDown is part of the family of OnMouseXXX events which are collider based and therefore only respond to the GameObject on which the click occurs. Not to Parent, not to Children and certainly not to any other GameObjects.

Only scripts on the receiving GameObject will be called, providing they have the OnMouseXXX event defined.

https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html

Shame there's no person-to-person chat on these forums. Would be so much easier to discuss these things off line and not clutter a question...

avatar image logicandchaos SurreyMuso · Oct 26, 2022 at 04:59 AM 0
Share

my bad I was thinking of Input.GetMouseButtonDown, haven't used OnMouseDown in so long I forgot about it!

Show more comments
avatar image
0

Answer by logicandchaos · Oct 24, 2022 at 01:18 PM

You can use t$$anonymous$$s interface to make GameObjects clickable

https://docs.unity3d.com/2019.1/Documentation/ScriptReference/EventSystems.IPointerClickHandler.html

Comment
Add comment · 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
0

Answer by NotlawGD · Oct 24, 2022 at 10:37 PM

The code looks fine to me. Just in case, have you checked that the object to click has the appropiate tag? Is the gameObject on a layer that's not Ignore Raycast? Is your script attached to any gameobject?

If you're using the New Input System, I believe you have to access Mouse.current.position.ReadValue() in order to get the right mouse position, but I'm not 100% sure.

Comment
Add comment · 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
0

Answer by Prqtection · Oct 27, 2022 at 01:27 AM

I still don't understand. Before, my OnMouseDown function was working, but now that's not even working. Here is my OnMouseDown code //code void OnMouseDown() { playerItems.wheat += 1; Debug.Log("OnMouseDown");

     }

I have a boxcollider2d as well, and it's not set to trigger.

Comment
Add comment · 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

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

216 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 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 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 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

OnMouseDown to return object name 3 Answers

I need help with raycasts and with color of gameobject [SOLVED] 3 Answers

OnMouseDown doesn´t work when the player looks down 1 Answer

Collider OnMouseDown movement lag 0 Answers

setting a bool on another object's animator by player's raycast 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