• 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
Question by lumpydumpy · Jun 03, 2013 at 10:09 PM · iteratemouse click

Click Event Implementation Problem

I have t$$anonymous$$s Clicked behaviour attached to a camera:

 public class Clicked : MonoBehaviour {    
     
     //stores the object that received MouseDown for consumption by the iterator
     private RaycastHit originalHit;
     
     void Update () {
         if (!Input.GetMouseButtonDown(0)){
             //mouse not being clicked
             return;
         }
 
         var camera = Camera.main;
         
         if (!Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), out originalHit, 100)){
             print ("no object being clicked by mouse");
             return;
         }
         
         var eventsBehaviour = originalHit.transform.GetComponent<SubscribedEvents>();
         if(eventsBehaviour == null 
             || !(eventsBehaviour is ISubscribeClicked)){
             print ("no event subscriptions or does not subscribe to Clicked event");
             return;
         }
         
         StartCoroutine(WaitOnUp(originalHit));
     }
     
     //waits on the mouse to be released on t$$anonymous$$s object so that it only acts
     //upon a mouse click that was completely on t$$anonymous$$s object
     IEnumerator WaitOnUp(RaycastHit $$anonymous$$t){
         w$$anonymous$$le (!Input.GetMouseButtonUp(0)){
             yield return 0;
         }
         if(!Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), out $$anonymous$$t, 100)){
             print ("MouseUp not on an object");
         }
         else if($$anonymous$$t.GetHashCode () != originalHit.GetHashCode ()){
             print("MouseUp on a different object");
         }
         else{
             //object received MouseDown and then MouseUp, so fire its OnClicked event
             print ("MouseUp on original object");
             (originalHit.transform.GetComponent<SubscribedEvents>() as ISubscribeClicked).OnClicked();
         }
     }
 }

   

However, if I quickly move the mouse to a different object after ensuring that MouseUp has occurred on the original object, I will see "MouseUp on a different object". Due to seeing t$$anonymous$$s, it seems like calling

 yield return 0

causes the program to not re-enter into the iterator every frame, as in it just skips some frames before re-entering. Is that true?

Comment

People who like this

0 Show 2
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 darthbator · Jun 03, 2013 at 10:40 PM 0
Share

have you tried yielding a null object rather then 0?

avatar image lumpydumpy · Jun 03, 2013 at 10:53 PM 0
Share

From what I think I remember reading, return 0 means come back next frame, and return null means come back in the same frame, which I don't want. However, in my desperation I tried it anyway and it didn't matter.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by lumpydumpy · Jun 03, 2013 at 11:56 PM

I thought GetHashCode was implemented for RaycastHit. Apparently it wasn't, and thus my comparison at line 38 was failing whenever the mouse moved between MouseDown and MouseUp. GetHashCode is incorrect to use anyway, but it would have not worked in a different way. I used $$anonymous$$t.transform.name instead, w$$anonymous$$ch probably isn't the best idea, but it will work for now.

Comment

People who like this

0 Show 0 · 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

15 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

Related Questions

Iterating through and deleting gameObjects 1 Answer

Count Variable Acting Strange 1 Answer

Iterating through shuffled/randomized array issue 0 Answers

Can I use the Input Manager with a specific "area click"? 1 Answer

iterate through array of objects with lists of objects 0 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