Romaldowoho
Romaldowoho

Reputation: 415

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.

Upvotes: 3

Views: 2240

Answers (1)

Robert Mock
Robert Mock

Reputation: 332

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.

Upvotes: 1

Related Questions