-1

What i try:

private void OnControllerColliderHit(ControllerColliderHit hit)
    {

        if (hit.gameObject.tag == "obstacle")
        {
            int lastRunScore = int.Parse(scoreScript.scoreText.text.ToString());
            PlayerPrefs.SetInt("lastRunScore", lastRunScore);
            anim.SetTrigger("isFall");
            speed = 0;
            
            UseDelay();
            
            losePanel.SetActive(true);
            PauseButton.interactable = false;
            
        }
        
        
    }
async Task UseDelay()
    {
        await Task.Delay(3000); 
    }

But nothing happen and I received such a warning

Assets\My new scripts\PlayerController.cs(171,13): warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the cal

Thanks in advance

  • You could always use `Inoke(“MethodNameInString”, 3f);`. Create a new method that does what you want, put the name — in a string — in the first argument, and put the time you want to wait (in seconds). Then it will call the method. – gbe May 10 '21 at 15:15

3 Answers3

0

When you are calling an async method, make sure to await it, as the compiler states

await UseDelay();
Zokka
  • 147
  • 6
  • This will make it synchronous (blocking the collision method) though and is not what OP wants to do in a frame based application such as Unity ;) – derHugo May 10 '21 at 15:34
0

Or you could use a coroutine (requires MonoBehaviour)

private void OnControllerColliderHit(ControllerColliderHit hit)
    {

        if (hit.gameObject.tag == "obstacle")
        {
            int lastRunScore = int.Parse(scoreScript.scoreText.text.ToString());
            PlayerPrefs.SetInt("lastRunScore", lastRunScore);
            anim.SetTrigger("isFall");
            speed = 0;
            
            StartCoroutine(UseDelay());
            
        }
        
        
    }
private IEnumerator UseDelay()
    {
        yield return new WaitForSeconds(3);
        losePanel.SetActive(true);
        PauseButton.interactable = false;
    }
mrxan
  • 51
  • 7
0

I added this. After that, everything worked. Thanksenter image description here