Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by crawler167 · Feb 21, 2017 at 01:50 AM · functionwaitforsecondsdelay

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);
         }

     }

 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by $$anonymous$$ · Feb 21, 2017 at 03:04 AM

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by crawler167 · Feb 21, 2017 at 06:29 PM

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: alt text

Already tried to fix but no luck,

Regards.


error.jpg (24.2 kB)
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Feb 21, 2017 at 08:11 PM 0
Share

Sorry. What I meant is to replace the void Update() with the IEnumerator CoUpdate. Both of these things are methods, but the IEnumerator returns a value (a delay). It should look something like this:

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class PlayerController2 : $$anonymous$$onoBehaviour {
  
      public float moveSpeed;
      public float jumpForce;
  
      private Rigidbody2D myRigidbody;
  
      public bool grounded;
      public Layer$$anonymous$$ask whatIsGround;
  
      private Collider2D myCollider;
  
      public Game$$anonymous$$anager theGame$$anonymous$$anager;
  
  
      // Use this for initialization
      void Start () {
          myRigidbody = GetComponent<Rigidbody2D> ();
  
          myCollider = GetComponent<Collider2D> ();
  
          StartCoroutine (CoUpdate());
  
  
      }
  
  
      IEnumerator CoUpdate()
      {
           while(true){
                grounded = Physics2D.IsTouchingLayers (myCollider, whatIsGround);
                myRigidbody.velocity = new Vector2 (moveSpeed, myRigidbody.velocity.y);
 
                if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space) || Input.Get$$anonymous$$ouseButtonDown (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") 
          {
              theGame$$anonymous$$anager.RestartGame ();
          }
  
          if (other.gameObject.tag == "enemy") 
          {
              myRigidbody.AddForce (new Vector3 (-7000f, 100f, transform.position.z));
          }
      }
  
  }

sorry for the confusion

avatar image crawler167 $$anonymous$$ · Feb 21, 2017 at 11:42 PM 0
Share

It doesn't give any errors. Seems ok, but when I press play the characters that has this script on just stands still, doing nothing.

Thank you for helping out anyway, if you know what is happening I'd love to know.

Regards.

avatar image crawler167 $$anonymous$$ · Feb 23, 2017 at 02:20 PM 0
Share

Can anyone still help me?

avatar image $$anonymous$$ · Feb 21, 2017 at 08:12 PM 0
Share

The error you were getting is that there is a method inside of a method (it's the debugger's weird way of telling you that)

avatar image crawler167 $$anonymous$$ · Feb 21, 2017 at 11:42 PM 0
Share

thanks for the help :)

avatar image
0

Answer by xdarkasgaming · Jun 24, 2020 at 08:30 PM

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;

 }


}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

95 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Delay in setting Boolean Value 1 Answer

prefab time delay 1 Answer

How to wait a certain amount of seconds in C# 4 Answers

For Loop does two loops at once! 0 Answers

need help for adding a delay code before walking of npc 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges