Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 mopaton362 · Jan 10, 2020 at 09:58 PM · startcoroutinemouseover

Stacking Coroutines with Mouseover

I want an event to trigger after a certain amount of hover time (3 s) has elapsed, and I'm using StartCoroutine coupled with a function returning an IEnumerator with yield return new WaitForSeconds(3.0f). However, I want to avoid the trigger if the user moves the cursor away from the triggering surface before the required time has passed.


There are two Button objects with the MouseHandler script attached to them, and with the GameManagerScript script referred to by gm.


What I have done is the following:


 public class MouseHandler : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
 {
     public GameManagerScript gm;
     
     public void OnPointerEnter(PointerEventData eventData)
     {  
         GameManagerScript.dateOffset = DateTimeOffset.UtcNow;
         GameManagerScript.outside = false;
         StartCoroutine(CallFunction(eventData));
     }
  
     public void OnPointerExit(PointerEventData eventData)
     {   
         GameManagerScript.dateOffset = DateTimeOffset.UtcNow;
         GameManagerScript.outside = true;
         gm.ButtonClick("out");
     }
     
     IEnumerator CallFunction(PointerEventData ed)
     {
         yield return new WaitForSeconds(3.0f);
         gm.ButtonClick(ed.pointerEnter.ToString());
     }
 }


Next, in my GameManagerScript I have the following, with a Text object that displays a certain string of characters, such as the name of the Text objects in both Button hierarchies, being referred to by t:


 public class GameManagerScript : MonoBehaviour
 {
     public Text t;
     
     public static bool outside = true; // in case entering and exiting occurs simultaneously
     public static DateTimeOffset dateOffset;
 
     // Start is called before the first frame update
     void Start()
     {
         t.text = "still nothing";
     }
     
     public void ButtonClick(string s)
     {
         long diff = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() -
                     dateOffset.ToUnixTimeMilliseconds();
         Debug.Log(diff);
         if (outside == false && diff >= 3000L) {
             t.text = s;
         }
     }
 }

This code and everything works fine, sort of. Whenever I load the executable for the first time, however, and hover my cursor over one of the buttons for 3 or more seconds, the label field will continue to show the predefined string still nothing. When I remove the cursor and try again, it works, and everything works fine then.


I'd like to get rid of this initial failure at the beginning of my program.


Second of all, I occasionally get this mysterious error in the console which doesn't seem to cause any trouble (although I can't pinpoint when it happens exactly): UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17).

I can't really reproduce it, but it originates on this line in the CallFunction function:

gm.ButtonClick(ed.pointerEnter.ToString());


Third of all, I'd like it if someone could comment on this particular solution of this problem. Is there a potential problem with it? I've tried quickly moving the cursor between the two buttons, thus causing the frequent function calls and such, but I haven't noticed any real issues. It's definitely a lazy solution, but I figured it can't be that bad.


Thanks for any help in advance.

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

0 Replies

· Add your reply
  • Sort: 

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

117 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

Related Questions

effect of an object when mouseover 1 Answer

I am making a MouseOver function and its not working 0 Answers

Put sprite next to button on mouse over, 1 Answer

StartCoroutine in a separate class 3 Answers

Problems with yield WaitForSeconds(); (IENumerator) 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