Coroutine, IEnumerator and Update issues

Hey guys I have been working on this procedure for over a day and I am about at the end of my sanity…

Basically I have 2 boxes, 1 is a wall and 1 is a cube. I am trying to create a tooltip that last for a few seconds and then disappears until the move leaves the object and points to it again. So far i have got the tool tip to disappear like i wanted however after numerous seconds the Coroutine starts fight with update and starts popping in and out. I read that your not supposed to start Coroutine within Update but start however i have tried both void start and IEnumerator start with zero success of the Coroutine starting (the text never fades).

I am quite new to unity and been checking out the tutorials and examples from here and various sites but at this point i am afraid i may have got myself confused or mixed up along the way and am just lost at this point. any advice or tips would be a godsend.

Here is the script I have been working with:

public GameObject textcanvas;
private int box;

// Use this for initialization
void Start()
{
textcanvas.SetActive(false);
box = LayerMask.NameToLayer(“box”);

}

// Update is called once per frame
void Update()
{
RaycastHit hit;

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

if (Physics.Raycast(ray, out hit))
{
if (hit.transform.gameObject.layer == box)
{
StartCoroutine(fade());

textcanvas.SetActive(true);
print(“box hit”);

}
}
}
private IEnumerator fade()
{
RaycastHit hit;

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

if (Physics.Raycast(ray, out hit))
{

if (hit.transform.gameObject.layer == box)
{
yield return new WaitForSeconds(3);
{
textcanvas.SetActive(false);

}
}
}
}
}

Use code tags, more likely to get help. Update runs several times per second, so if you trigger a coroutine, you are most likely triggering it several times. the chance that you mouse over an object for 1 update call only is highly unlikely.

I’m also unsure why you are checking in your update and your coroutine for ray hits. Chances are, this is why your text doesn’t turn off. Just feels like a timing issue.

What you might want to do is in your update, have a bool (isTooltip) that you set to true, so it gets set to true the first time. Then you check that value, if true, don’t call your coroutine. Heck, you could probably even not do the raycasting at that point to save a little.

In the coroutine, you want to do your yield WaitForSeconds. At the end of those second, check your raycast. If the mouse is still over the target, you can either restart the coroutine or just create some sort of loop. If the mouse isn’t over it, then you’ll turn it off.

Just a thought on how you might set this up differently.

1 Like

I would solve the problem with a timer. Start it in OnPointerExit. Reset the timer in OnPointerEnter.

No. Update runs exactly once per frame.

1 Like

Sorry, meant per secs. Fixed. And yep, a timer would work as well. I think I used a timer on a tooltip before.

2 Likes

@Brathnann @Kiwasi thank you so very much and sorry for my late response (crashed after I left the office frustrated yesterday). Thank you so much, I am going to look into trying a timer instead. @Brathnann Thank you so much for you hints, tips and advice, being a plain 3D modeler this is completely new ground for me and I am still trying to learn the ropes in unity and this forum.

Just wanted to say THANK YOU!!!

It worked like a charm, works WAY better then the way raycast was, proper time, no overlapping text, quicker response time and a code easily much shorter and simpler. And thank you for just showing me a better and alternate way of executing thing. Thanks again

@Brathnann @Kiwasi

Retarded question the Appear and disappear worked great however I tried to expand by making the tooltip follow the mouse position while over the object, however doing so seems to turn the text on and off and after looking at the log even though the mouse is over the object it still registers as the object entering and exit. Before to get the poistion to follow mouse I used the following in the OnPointerEnter area (fuction is proper term?):
RightText.transform.position = Input.mousePosition;

I have tried creating a seperate event for it, such as putting it under update, OnPointerUp. Am I just missing something? here is an example of the revised script

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class BackupBarTooltip : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerUpHandler
{
    public GameObject RightText;
    private bool Appear = false;
    void Start()
    {
        RightText.SetActive(false);
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        Appear = true;
        RightText.SetActive(true);
        StartCoroutine("Vanish");
        RightText.transform.position = Input.mousePosition;

        Debug.Log("Pointer Enter");
    }
    public void OnPointerUp(PointerEventData eventData)
    {
        RightText.transform.position = Input.mousePosition;

    }

    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("Pointer Exit");
        RightText.SetActive(false);

    }
    private IEnumerator Vanish()
    {
        if (Appear)
        {
            yield return new WaitForSeconds(4);
            RightText.SetActive(false);

        }
    }
}

Use code tags

Ah ok thank again sorry.

You could start a coroutine that follows the mouse while Appear is true (don’t forget to set appear to false on exit), but be careful that your tooltip doesn’t intercept, which will trigger the OnPointerExit, thus would then retrigger your OnPointerEnter when it turns off.

Thanks again for the suggestion and the advice. Your advice was very quick and easy however I am an idiot and tried to make it more complicated (lesson learned)
Turns out a simple click of the “turn off Raycast Target” button under the text was all that was needed…(I am an idiot ) lol

just in case anyone else this issue here is the script that i got to finally work flawlessly:

{
    public GameObject HydraulicPressText;
    private bool Appear = false;
    private bool Disappear = true;
    void Start()
    {
        HydraulicPressText.SetActive(false);
    }
    public void OnPointerEnter(PointerEventData eventData)
    {
        Appear = true;
        HydraulicPressText.SetActive(true);
        StartCoroutine("Vanish");
        Debug.Log("Pointer Enter");
    }
    public void OnPointerExit(PointerEventData eventData)
    {

        Disappear = true;
        Appear = false;
        Debug.Log("Pointer Exit");
        HydraulicPressText.SetActive(false);

    }
    void Update()
    {
        HydraulicPressText.transform.position = Input.mousePosition;

    }
    private IEnumerator Vanish()
    {

        if (Appear == true)
        {
            yield return new WaitForSeconds(3.0f);
                HydraulicPressText.SetActive(false);
            if (Appear == false)
            {
                HydraulicPressText.SetActive(false);
            yield break;


            }
        }
    }
}

Yep, sorry, guess I should have mentioned the raycast checkbox. I knew that was the issue because I had a similar problem with tooltips I created a while back. Glad you finally got it working!

1 Like