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
1
Question by K1rus · Nov 26, 2016 at 01:55 PM · c#scripting beginner

How to make a delay before an action?

Hello! I'm new to Unity and C#. Need some help with making a delay before doing something.

I need to wait for 3 seconds before application runs next scene.

Here is my script:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     public float speed;
     public Text pickupsLeftText;
     public Text winText;
     public string nextLevel;
 
     private Rigidbody rb;
     private int pickupsLeftNum;
     private int pickups;
     private bool levelFinished;
 
     void Start ()
     {
         rb = GetComponent<Rigidbody>();
         GameObject[] pickupsOnLevel = GameObject.FindGameObjectsWithTag ("Pickup");
         pickups = pickupsOnLevel.Length;
         pickupsLeftNum = pickups;
         SetPickupsLeftText ();
         winText.text = "";
         levelFinished = false;
     }
 
     void Update () {
         if (levelFinished) {
             StopCoroutine ("NextLevel");
             StartCoroutine ("NextLevel");
         }
     }
 
     void FixedUpdate ()
     {
 
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
 
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
 
         rb.AddForce (movement * speed);
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag("Pickup")) 
         {
             other.gameObject.SetActive(false);
             pickupsLeftNum = pickupsLeftNum - 1;
             SetPickupsLeftText ();
         }
 
         if (other.gameObject.CompareTag("Speed Up")) 
         {
             other.gameObject.SetActive(false);
             speed = speed * 2;
         }
 
 
     }
         
     void SetPickupsLeftText ()
     {
         pickupsLeftText.text = "Pickups left: " + pickupsLeftNum;
 
         if (pickupsLeftNum == 0) {
             levelFinished = true;
             winText.text = "You win!";
             NextLevel ();
         }
     }
 
     void NextLevel ()
     {
         yield WaitForSeconds(3);
         Application.LoadLevel (nextLevel);
     }
 }

I need to have 3 seconds delay in NextLevel function. Thank you!

P.S. Could you please advice me some websites to learn C#?)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by vaniko2003 · Aug 23, 2017 at 06:26 PM

You just have to go to a new void in time with Invoke, like:

     if(input.GetKeyDown(Keycode.W)){ 
     Invoke("newVoid", 2); //2 is the time
     }
     
     void newVoid()
     {
     //things you wanna do after 2 seconds...
     }
     
     //this delay will happen, when you press W key!

Hope it helps!

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 hexagonius · Nov 26, 2016 at 02:25 PM

close enough, it's

 yield return new WaitForSeconds(3);

the unity scripting API is a good place to read about how to use functions

Comment
Add comment · Show 2 · 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 K1rus · Nov 26, 2016 at 03:57 PM 0
Share

The body of PlayerController.NextLevel()' cannot be an iterator block because void' is not an iterator interface type.

avatar image hexagonius K1rus · Nov 27, 2016 at 12:18 AM 0
Share

oh whoops, didn't see that. ins$$anonymous$$d of void the method needs to return IEnumerator

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

269 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 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 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

I'm trying to create a script to store a number of colors and when a gameObject is selected it would display a menu and user can choose what color to assign it. 0 Answers

Best way to learn how to utilize the Unity API? 2 Answers

Get Childrens to new array from perents array. 1 Answer

How would I implement obstacle avoidance in this script? I am clueless. 1 Answer

Check If System Month IS Between A Certain Range 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