Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using yield WaitForSeconds() in a function returning void

I have a jumpscare in my game. I am using Unity 3D. My first function is

public void ScareMe(Vector3 pos) {
    //it does some necessary irrelevant 
    //stuff and then it invokes another function
    Invoke ("Smile",.2f);
}

In my other function I want to make an object appear and then disappear in 0.2 ms. I use

 IEnumerator Smile() {
     Object.SetActive(true);
     yield return new WaitForSeconds(0.2f);
     Object.SetActive(false);
 }

But for some reason my function Smile is never invoked as long as it returns something other then void.

Is there any way to use something like yield but go around it so I don't have to return anything? I was thinking a while loop like a coroutine? But I am not sure how to go about it.

like image 409
Romaldowoho Avatar asked Feb 26 '26 01:02

Romaldowoho


1 Answers

If I remember correctly, Unity's Invoke(func) is only willing to start a coroutine in Javascript, where the difference between a void and a coroutine is less strict.

What I would do is use StartCoroutine(Smile()); and start Smile with another yield return new WaitForSeconds(0.2f);, or better yet have Smile take a float smileDelay parameter and use that for your WaitForSeconds.

like image 136
Robert Mock Avatar answered Feb 28 '26 17:02

Robert Mock