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 zackmah · Jun 28, 2016 at 05:56 PM · waitforsecondsload level

I want to delay 3 sec to load next level after the collision in C#

I need to delay the load new level by a few second. Please help.This is the script I need to use:

public class myscore : MonoBehaviour {

void OnCollisionEnter2D(Collision2D col){

     Debug.Log ("Collision");

     if (col.gameObject.tag == "Player") {
         
         score = score + 50;
         Destroy (gameObject);
        *Here I need to wait for 3 sec then it should load the next level*
         SceneManager.LoadScene(nextLevel);

} }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by PrisVas · Jun 28, 2016 at 07:36 PM

Use StartCoroutine:

     *Here I need to wait for 3 sec then it should load the next level*
     StartCoroutine(WaitForIt(3.0F));
      //SceneManager.LoadScene(nextLevel);


     IEnumerator WaitForIt(float waitTime) {
         yield return new WaitForSeconds(waitTime);
         SceneManager.LoadScene(nextLevel);

}

Comment
Add comment · Show 4 · 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 zackmah · Jun 28, 2016 at 09:53 PM 1
Share

Hi, still it doesn't work. If I write this code then my score is not updating and next level is not loading :( . Please help. Thanks

avatar image PrisVas zackmah · Jun 28, 2016 at 10:03 PM 0
Share

I see. It's not working because you are destroying the object before it finishes the others instructions.

You can put the OnCollisionEnter2D and WaitForIt on the player script, so you can destroy this object with no problem. Or you can hide the object while waiting for the 3 secs.

avatar image zackmah · Jun 28, 2016 at 11:51 PM 0
Share

Hi, my score is updating and next level is loading in the OnCollisionEnter2D method with my old code but when I add this code then nothing is working. I tried several ways but it didn't work :(. right now it looks like this :

        void OnCollisionEnter2D(Collision2D col){
 
         Debug.Log ("Collision");
 
 
             if (col.gameObject.tag == "Enemy") {
             
                 score = score + 50;
                 Destroy (gameObject);
 
               StartCoroutine (WaitForIt (3.0F)); 
 }
 }
     IEnumerator WaitForIt(float waitTime) {
         yield return new WaitForSeconds(waitTime);
         Scene$$anonymous$$anager.LoadScene(nextLevel);
     }
   



avatar image PrisVas zackmah · Jun 29, 2016 at 01:08 AM 0
Share

Yours works because it is instant. But you want to wait 3 seconds. so you can't destroy the object and and do something 3 seconds later. An exemple: Imagine you and your mother in the same room, so you say "mom, bring me some water, please" and just after you say: "bring me some cookies too". But then imagine you say: "mom, bring me some water, please" and wait 3 seconds, say: "bring me some cookies too". She wont hear you, because she's already out of the room. So you have to destroy the object 3 seconds later. And I guess, if you are loading a new scene, you dont even need to destroy it, just load the nextLevel.

Comment the Destroy (gameObject); and will be fine.

But, if you REALLY need to disapear with this object, don't destroy it, do this:

 void OnCollisionEnter2D(Collision2D col)
 {
 
     Debug.Log("Collision");
 
     if (col.gameObject.tag == "Player")
     {
         score = score + 50;
         //Destroy(gameObject);
         gameObject.GetComponent<SpriteRenderer>().enabled = false;
         gameObject.GetComponent<Collider2D>().enabled = false;
     
         StartCoroutine(WaitForIt(3.0F));
     }
 }




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

59 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

Related Questions

Instantiate an object and destroy it after given time 2 Answers

Weather Timer (Beginner to scripting) (In multiplayer) 0 Answers

How to use IEnumerator? Pls help! 1 Answer

Wait in unity(coroutine not working) 0 Answers

how to change a value over time 1 Answer


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