How can I delay 1 second this code?

Hey guys,
I’ve been trying to delay this code below to execute only after 1 second, no luck:

I’d like it to jump only after 1 second.

I don’t know even the basics but I’m trying to learn. Sorry for that.

Regards.

void Update () {

	grounded = Physics2D.IsTouchingLayers (myCollider, whatIsGround);

	myRigidbody.velocity = new Vector2 (moveSpeed, myRigidbody.velocity.y);

	if (Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown (0)) 
	{

		if (grounded) {
			myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, jumpForce);
		}

	}

}

As far as I know, the only nice way to delay a piece of code in Unity is to use a Coroutine (or an IEnumerator rather). There are many ways to go about doing this, but I would do something like this:

IEnumerator CoUpdate (){
     grounded = Physics2D.IsTouchingLayers (myCollider, whatIsGround);
     myRigidbody.velocity = new Vector2 (moveSpeed, myRigidbody.velocity.y);
     if (Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown (0)) 
     {
         if (grounded) {
             // Tells Unity to wait for 1 second
             yield return new WaitForSeconds(1);
             myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, jumpForce);
         }
     }
     
     // Very important, this tells Unity to move onto next frame. Everything crashes without this
     yield return null;
 }

And then inside of your Start() or Awake() method include this line of code:

StartCoroutine(CoUpdate());

If your interested, I would read this. It talks about the basics of how Coroutines work. There lots of cool things you can do with it though like WaitUntil or WaitForSecondsRealtime. You can even create your own, but it’s not necessary for this.

Apologies for any syntax errors, I’m just writing it here so it’s possible I’ve made a mistake.

I hope this helped!

Loved your help, i’m really trying to learn.

my whole code is like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController2 : MonoBehaviour {

	public float moveSpeed;
	public float jumpForce;

	private Rigidbody2D myRigidbody;

	public bool grounded;
	public LayerMask whatIsGround;

	private Collider2D myCollider;

	public GameManager theGameManager;


	// Use this for initialization
	void Start () {
		myRigidbody = GetComponent<Rigidbody2D> ();

		myCollider = GetComponent<Collider2D> ();

		StartCoroutine (CoUpdate());


	}


	// Update is called once per frame
	void Update () {

		IEnumerator CoUpdate()
		{
		grounded = Physics2D.IsTouchingLayers (myCollider, whatIsGround);
		myRigidbody.velocity = new Vector2 (moveSpeed, myRigidbody.velocity.y);
		if (Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown (0)) 
		{
			if (grounded) {
				//Tells Unity to wait 1 second
					yield return new WaitForSeconds(1);
				myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, jumpForce);
			}

		}
			yield return null;		
	}
	void OnCollisionEnter2D (Collision2D other)
	{
		if (other.gameObject.tag == "killbox") 
		{
			theGameManager.RestartGame ();
		}

		if (other.gameObject.tag == "enemy") 
		{
			myRigidbody.AddForce (new Vector3 (-7000f, 100f, transform.position.z));
		}
	}

}

My only problem is that console is giving me these errors:

Already tried to fix but no luck,

Regards.

can you help me about my code, it didn’t work for me. All i want is when game object (player) is destroyed, my gameover panel will appear and totally paused the entire screen. actually it works the waitforseconds after the player die the gameover panel pop out, but the game itself continue running.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerGameOver : MonoBehaviour
{

public GameObject gameOverPanel, explosion, pauseButton, player;



public void Awake()
{
    
}

public void OnTriggerEnter2D(Collider2D other)
{

    if (other.tag.Equals("Enemy"))
    {
        if (Time.timeScale == 1f)
            Time.timeScale = 1f;
        else
            Time.timeScale = 0f;
        StartCoroutine(waitForGO());

        Instantiate(explosion, transform.position, Quaternion.identity);
        

        gameOverPanel.SetActive(false);
        FindObjectOfType<AudioManager1>().Play("PlayerDeath");
        
    }

    StartCoroutine(waitForGO());
}

IEnumerator waitForGO()
{
    yield return new WaitForSeconds(3);
    player.SetActive(false);
    gameOverPanel.SetActive(true);
    pauseButton.SetActive(false);
    
    Time.timeScale = 0f;

}

}