Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by automan64 · Jul 15, 2012 at 04:27 AM · yielddelaynext level

How can I delay Application.Loadlevel for a few seconds?

Hello. I am a beginner to Unity. My question is how do I delay my game moving to the game over scene for a few seconds, as I want a particle system to fully play out. After the enemy dies, the game instantly moves to the next scene (either You Win or You Lose). There is a massive explosion that happens when the enemy wins and I want this to play out. I have included my script (JavaScript). Thank you for any help! This is an edit as requested by fattie. (Sorry I missed your comment a few minutes ago.)

var enemyCharge: int; var HP: int; var enemyWins: Transform; var enemyPickup: Transform;

function OnGUI () { GUI.Label (Rect (10, 50, 100, 20), "Charges: " + enemyCharge); GUI.Label (Rect (10, 80, 100, 20), "Hit Points: " + HP); }

function OnTriggerEnter (collider: Collider) {

if(enemyCharge == 9) { //This is where the enemy wins

var tempenemyWins: Transform; tempenemyWins = Instantiate (enemyWins, transform.position, transform.rotation); Destroy (gameObject);

//Transition to lose screen. Put Invoke here

Invoke ("LoseScreen", 3); }

if(collider.gameObject.tag == "EnemyCharge") { var tempenemyPickup: Transform; tempenemyPickup = Instantiate (enemyPickup, transform.position, transform.rotation); enemyCharge ++; collider.gameObject.transform.position.y = 17; collider.gameObject.transform.position.x = Random.Range (-20,15); } if(collider.gameObject.tag == "Bullet") { HP --; } }

function Update () { if(HP == 0) {

//This is where the player wins

Destroy (gameObject);

//Transition to win screen

Application.LoadLevel(3); }

// Invoke function itself

function LoseScreen() { Application.LoadLevel(0); }

}

Comment
Add comment · Show 1
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 Fattie · Jul 15, 2012 at 07:27 AM 0
Share

if you're just getting started, learn about the Invoke command. enjoy!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Jul 15, 2012 at 05:06 AM

The easiest way is to call a coroutine that waits for the specified time before loading the next level - like this:

... function Update () { if(HP == 0) { //This is where the player wins Destroy (gameObject); LoadLevel(3, 2.5); // call the coroutine } }

function LoadLevel(level: int, delay: float){ // wait the delay time... yield WaitForSeconds(delay); // then load the level: Application.LoadLevel(level); }

Comment
Add comment · Show 9 · 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 automan64 · Jul 15, 2012 at 05:49 AM 0
Share

Thanks for responding! However I cannot compile as function Update() cannot have a coroutine.

avatar image automan64 · Jul 15, 2012 at 06:11 AM 0
Share

I also tried to copy and paste your code, but the error says "No Appropriate version of 'Unity.Application.LoadLevel' for this argument list. I am not really sure what that means.

avatar image whydoidoit · Jul 15, 2012 at 07:27 AM 0
Share

You have to have pasted his coroutine wrong - Update is not a coroutine in @aldonaletto's answer

avatar image Fattie · Jul 15, 2012 at 01:09 PM 0
Share

I urge you to simply try using "Invoke".

It is a very very very simple command, unity aded for exactly this purpose! Hope it helps!

avatar image automan64 · Jul 15, 2012 at 04:57 PM 0
Share

Hi. Perhaps I am not doing something right with the Invoke command? The game would not transition to the lose screen when the enemy won. I am still learning program$$anonymous$$g so maybe I did something wrong with Invoke.

var enemyCharge: int; var HP: int; var enemyWins: Transform; var enemyPickup: Transform;

function OnGUI () { GUI.Label (Rect (10, 50, 100, 20), "Charges: " + enemyCharge); GUI.Label (Rect (10, 80, 100, 20), "Hit Points: " + HP); }

function OnTriggerEnter (collider: Collider) {

if(enemyCharge == 9) { //This is where the enemy wins

var tempenemyWins: Transform; tempenemyWins = Instantiate (enemyWins, transform.position, transform.rotation); Destroy (gameObject);

//Transition to lose screen. Put Invoke here

Invoke("LoseScreen", 2.5);

}

if(collider.gameObject.tag == "EnemyCharge") { var tempenemyPickup: Transform; tempenemyPickup = Instantiate (enemyPickup, transform.position, transform.rotation); enemyCharge ++; collider.gameObject.transform.position.y = 17; collider.gameObject.transform.position.x = Random.Range (-20,15); } if(collider.gameObject.tag == "Bullet") { HP --; } }

function Update () { if(HP == 0) { //This is where the player wins

Destroy (gameObject);

//Transition to win screen

Application.LoadLevel(3); }

} //The invoke function itself

function LoseScreen() { Application.LoadLevel(0); }

Show more comments
avatar image
0

Answer by delstrega · Jul 15, 2012 at 05:08 AM

Add

 yield WaitForSeconds (2.5);

before the Application.LoadLevel(...) where "2.5" is the time in seconds to wait.

This will exit the execution of your OnTriggerEnter, wait for 2.5 seconds, then reenter OnTriggerEnter in exactly the position it left off of. This means it will execute everything after your "yield WaitForSeconds(2.5)" when it returns.

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 automan64 · Jul 15, 2012 at 05:48 AM 0
Share

Thanks for responding! However, when is add yield WaitForSeconds (2.5); before Application.LoadLevel(0); the game does not transition to the lose screen at all.

avatar image delstrega · Jul 15, 2012 at 07:34 AM 0
Share

$$anonymous$$ight have something to do with OnTriggerEnter not liking to be a coroutine. Try Aldo Netto's approach which is basically the same - difference being an extra function created.

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

8 People are following this question.

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

Related Questions

Have a function wait unitl a boolean is false 1 Answer

restart level when dead with delay 3 Answers

Coroutine not running after yield return new WaitForSeconds 3 Answers

Create a delay between function executions 1 Answer

Yield only working once? 0 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