Home > Enterprise >  How can I declare an async in c#?
How can I declare an async in c#?

Time:07-02

Sorry if this is a stupid question, but i'm stuck on making a respawn system that respawns after five seconds. This is my code so far

    public async ow()
    {
        Debug.Log("Readying Respawn");
        gameover.SetActive(true);
        blur.SetActive(true);

        await Task.Delay(5000);
        Debug.Log("Respawning!");
            player.transform.position = respawn.position;
        gameover.SetActive(false);
        blur.SetActive(false);

    }

I did the async to fix this error:

Assets\Death.cs(27,9): error CS4033: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.

But that returns

Assets\Death.cs(21,12): error CS0246: The type or namespace name 'async' could not be found (are you missing a using directive or an assembly reference?)

and I got stuck on that part.

I am also using Unity Engine if that helps.

CodePudding user response:

I found the answer to my question here. After more research and various tests, this returns no errors. I just had to use the Task method. Hope this helps anyone else stuck on this.

CodePudding user response:

async method should have return type Task :

public async Task ow()
{
    Debug.Log("Readying Respawn");
    gameover.SetActive(true);
    blur.SetActive(true);

    await Task.Delay(5000);
    Debug.Log("Respawning!");
        player.transform.position = respawn.position;
    gameover.SetActive(false);
    blur.SetActive(false);

}
  • Related