Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 lumpydumpy · Jun 03, 2013 at 10:09 PM · iteratemouse click

Click Event Implementation Problem

I have this 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 this object so that it only acts
     //upon a mouse click that was completely on this object
     IEnumerator WaitOnUp(RaycastHit hit){
         while (!Input.GetMouseButtonUp(0)){
             yield return 0;
         }
         if(!Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), out hit, 100)){
             print ("MouseUp not on an object");
         }
         else if(hit.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 this, 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
Add comment · 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
0
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 hit.transform.name instead, which probably isn't the best idea, but it will work for now.

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.

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

Iterate an array 4 by 4 1 Answer

Coroutine being iterated in an unrelated function 0 Answers

Help doing mouse click changin shader and back to normal to a gameObject ꜛ! 1 Answer

How to determine what type of class is being interated through in an array of multiple classes 1 Answer

how do I generate a drop-down list of functions on scripts attached to a game object? 1 Answer


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