- Home /
IEnumerator Coroutine 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);
}
}
}
}
}
Answer by vintar · Mar 03, 2017 at 08:44 PM
You need to get a reference to your IEnumerator and see if it is running before calling it again like so : (not the full code, just a guide)
IEnumerator _fade;
if (hit.transform.gameObject.layer == box)
{
CheckFade();
print("box hit");
}
void CheckFade()
{
if(_fade == null)
{
_fade = fade;
textcanvas.SetActive(true);
StartCoroutine(_fade);
}
}
if (hit.transform.gameObject.layer == box)
{
yield return new WaitForSeconds(3);
{
textcanvas.SetActive(false);
_fade = null;
}
}
You have to execute your generator method "fade" in order to get an IEnumerator object:
_fade = fade(); // line 13
Also i would always recommend to use a Coroutine variable ins$$anonymous$$d of an IEnumerator. It's easier to use. Btw: In this case you don't need to store the coroutine as a simple bool variable would work as well.
Answer by ExtinctSpecie · Mar 07, 2017 at 11:33 AM
instead of calling a co-routine for your delays you can try making fade a simple method
float timer = 3f;
timer -= Time.deltaTime();
if(timer
Your answer
Follow this Question
Related Questions
Instantiate Particles, WaitForSeconds and continue 1 Answer
Execute IEnumerator In 1 Frame 4 Answers
Fire rate for gun script, c# (not u/s) 1 Answer
c# waitforsecconds 2 Answers
Unsolvable issue with System.Action and IEnumerator 0 Answers