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
0
Question by cj31387 · Jun 15, 2012 at 07:29 AM · timerloadlevelwaitforsecondsdelay

Better way for a timer to load a level?

Currently I'm using waitforseconds to delay my level loading controlled by a if statement if the score is a certain score per level, but what is happening is the coroutine is getting called like 10 or more times before that statement is false. I was thinking i could just replace waitforseconds with invoke, then do all i need to do to load the level in those functions but wouldn't those get called like 10 or more times too if they are still in the update function? How would i go about getting a delay for the level to load while constantly checking the score and then only calling a invoke once or waitforseconds once? here is my current code. Thanks.

 public static  ScoreBoard  global;
 public static float Score = 0f;
 public static float Lives = 3f;
 public static bool lvlChange = false;
 public static float Timer = 0;
 public static bool countdown = false;
 public GameObject spawnHolder;
 public static bool Respawning = false;
 public static bool wonGame = false;
 //public static bool paddleDieLvlChange = false;
 // Use this for initialization
 void Start () {
 //global=this;
 }
 
 // Update is called once per frame
 void Update () {
     if (Application.loadedLevelName == "Level1" && Score >= 280) // should be 330
     {
         StartCoroutine(IntermissionL1());                        
         Score = 0f;
     }

     if (Application.loadedLevelName == "Level2" && Score >= 360) // should be 360
     {
         StartCoroutine(IntermissionL2());
         Score = 0f;
     }

     if (Application.loadedLevelName == "Level3" && Score >= 370) // should be 440
     {
         StartCoroutine(IntermissionL3());
         Score = 0f;
     }

     if (Application.loadedLevelName == "Level4" && Score >= 430) // should be 430
     {
         StartCoroutine(IntermissionL4());
         Score = 0f;
     }

     if (Application.loadedLevelName == "Level5" && Score >= 530) // should be 530
     {
         StartCoroutine(IntermissionL5());
         Score = 0f;
     }

     if (Application.loadedLevelName == "Level6" && Score >= 470) // should be 470
     {
         StartCoroutine(IntermissionL6());
         Score = 0f;
     }

     if (Application.loadedLevelName == "Level7" && Score >= 280) // should be 280
     {
         StartCoroutine(IntermissionL7());
         Score = 0f;
     }

     if (Application.loadedLevelName == "Level8" && Score >= 340) // should be 340
     {
         StartCoroutine(IntermissionL8());
         Score = 0f;
     }

     if (Application.loadedLevelName == "YouWon") 
     {
         StartCoroutine(YouWon());
         wonGame = true;
         Score = 0f;
     }

     if (Lives == 0f)
     {
         Application.LoadLevel("GameOver");
         Score = 0f;
         Lives = 3f;
     }

     if (Ball_Movement.playerDead == true)
     {
         StartCoroutine(Countdown());
         
     }
     
     
 }
 // Makes a GUI from the variable score which needs to be tied to when a block dies.
 void OnGUI()
 {
     GUI.Box(new Rect(10, 430, 100, 20), "" + Score);
     GUI.Box(new Rect(350, 430, 100, 20), "" + Lives);
     if (countdown)
     {
         GUI.Box(new Rect(180, 400, 100, 20), "Respawning!!"); 

     }
     if (lvlChange)
     {
         GUI.Box(new Rect(40, 400, 500, 20), "You Won! Changing levels in 5 Seconds...");

     }

     if (wonGame)
     {
         GUI.Box(new Rect(40, 300, 300, 20), "You Beat the Whole Game WOOHOO!!!...");

     }
 }
 private IEnumerator Countdown()
 {
     Ball_Movement.playerDead = false;
     countdown = true;
     yield return new WaitForSeconds(5f);
     countdown = false;
     RespawnPlayerAndBall();
 }

 private IEnumerator IntermissionL1()
 {
     
     Destroy(PaddleMove.Instance.gameObject);
     Destroy(Ball_Movement.instance.gameObject);
     lvlChange = true;
     yield return new WaitForSeconds(5f);       
     Application.LoadLevel("Level2");
     lvlChange = false;

 }

 private IEnumerator IntermissionL2()
 {
     Destroy(PaddleMove.Instance.gameObject);
     Destroy(Ball_Movement.instance.gameObject);
     lvlChange = true;
     yield return new WaitForSeconds(5f);   
     Application.LoadLevel("Level3");
     lvlChange = false;

 }

 private IEnumerator IntermissionL3()
 {
     Destroy(PaddleMove.Instance.gameObject);
     Destroy(Ball_Movement.instance.gameObject);
     lvlChange = true;
     yield return new WaitForSeconds(5f);   
     Application.LoadLevel("Level4");
     lvlChange = false;

 }

 private IEnumerator IntermissionL4()
 {
     Destroy(PaddleMove.Instance.gameObject);
     Destroy(Ball_Movement.instance.gameObject);
     lvlChange = true;
     yield return new WaitForSeconds(5f);
     //Ball_Movement.playerDead = false;
     Application.LoadLevel("Level5");
     lvlChange = false;

 }

 private IEnumerator IntermissionL5()
 {
     Destroy(PaddleMove.Instance.gameObject);
     Destroy(Ball_Movement.instance.gameObject);
     lvlChange = true;
     yield return new WaitForSeconds(5f);
     //Ball_Movement.playerDead = false;
     Application.LoadLevel("Level6");
     lvlChange = false;

 }

 private IEnumerator IntermissionL6()
 {
     Destroy(PaddleMove.Instance.gameObject);
     Destroy(Ball_Movement.instance.gameObject);
     lvlChange = true;
     yield return new WaitForSeconds(5f);
     //Ball_Movement.playerDead = false;
     Application.LoadLevel("Level7");
     lvlChange = false;

 }

 private IEnumerator IntermissionL7()
 {
     Destroy(PaddleMove.Instance.gameObject);
     Destroy(Ball_Movement.instance.gameObject);
     lvlChange = true;
     yield return new WaitForSeconds(5f);
     //Ball_Movement.playerDead = false;
     Application.LoadLevel("Level8");
     lvlChange = false;

 }
 private IEnumerator IntermissionL8()
 {
     Destroy(PaddleMove.Instance.gameObject);
     Destroy(Ball_Movement.instance.gameObject);
     lvlChange = true;
     yield return new WaitForSeconds(5f);
     //Ball_Movement.playerDead = false;
     Application.LoadLevel("YouWon");
     lvlChange = false;

 }

 private IEnumerator YouWon()
 {
     wonGame = true;
     yield return new WaitForSeconds(7f);
     Application.LoadLevel("Credits");
     wonGame = false;

 }

 void RespawnPlayerAndBall()
 {
     GameObject paddleSpawn = (GameObject)Instantiate(spawnHolder, new Vector3(0, 1, -15), transform.rotation);
     Respawning=true;
 }

}

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 GC1983 · Jun 15, 2012 at 07:35 AM 0
Share

Trying using boolean triggers with time.deltaTime.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jun 15, 2012 at 09:05 AM

You could (or should) use parameters instead of 8 equal coroutines. Your Intermission coroutine should look more like this:

 private IEnumerator Intermission(string aLevel, float aDelay)
 {
     Destroy(PaddleMove.Instance.gameObject);
     Destroy(Ball_Movement.instance.gameObject);
     lvlChange = true;
     yield return new WaitForSeconds(aDelay);
     Score = 0.0f;
     Application.LoadLevel(aLevel);
     lvlChange = false;
 }

Also you have already your lvlChange variable which could be used in Update to not call the end levelchecks when a level change is already triggered.

Anyway, i would do it like this:

 public string[] levels = new string[]
 {
     "Level1",
     "Level2",
     "Level3",
     "Level4",
     "Level5",
     "Level6",
     "Level7",
     "Level8",
     "YouWon"
 }
 public float[] levelScores = new float[]
 {
     280.0f,  // level 1
     360.0f,  // level 2
     370.0f,  // level 3
     430.0f,  // level 4
     530.0f,  // level 5
     470.0f,  // level 6
     280.0f,  // level 7
     340.0f,  // level 8
     0.0f     // end
 }
 public static int currentLevel = 0;
 void Start()
 {
     StartCoroutine(WinCheck());
 }
 IEnumerator WinCheck()
 {
     while(true)
     {
         if (currentLevel < levels.Length-1)
         {
             if (Score >= levelScores[currentLevel])
             {
                 currentLevel++;
                 yield return Startcoroutine(Intermission(levels[currentLevel], 5.0f));
             }
         }
         else
         {
             yield return StartCoroutine(YouWon());
         }
         yield return null;
     }
 }
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 cj31387 · Jun 15, 2012 at 10:30 AM 0
Share

I'm too new at program$$anonymous$$g to read and fully understand your example. But as is it loads the next level immediately. I tried launching it with lvlChange in a if statement in update but it says array index out of range, that way.

avatar image cj31387 · Jun 16, 2012 at 04:58 AM 0
Share

Still stuck on this one.

avatar image Bunny83 · Jun 17, 2012 at 01:22 AM 0
Share

Sorry, got the limit wrong since i increment first. Fixed it ;)

I will add some explanations....

avatar image Bunny83 · Jun 17, 2012 at 01:37 AM 0
Share

Here some explanations:

 IEnumerator WinCheck()
 {
     while(true)
     {
         // ...
         yield return null;
     }
 }

This is actually something similar to Update, but since it's a coroutine we can also interrupt it at anytime. This little piece of code will run every frame. yield return null will yield until the next frame, therefore it's executed once a frame like Update.

 if (currentLevel < levels.Length-1)
 {
     // we still have levels to go
 }
 else
 {
     yield return StartCoroutine(YouWon());
 }

This will check if there are still levels left. If we reached the last one (the won level) we execute the YouWon coroutine. Note that when you yield return another coroutine like i did here, the outer coroutine will wait until the inner coroutine has finished. So WinCheck will be suspended until YouWon() or Intermission() is finished.

 if (Score >= levelScores[currentLevel])
 {
     currentLevel++;
     yield return Startcoroutine(Intermission(levels[currentLevel], 5.0f));
 }

This checks if the player reached the required score for the current level. If the condition is true we just increment the current level and call our intermission coroutine which will wait 5 seconds and then load the new current level.

If you are more comfortable with your Update "construct" ;) You would have to do something like that:

 void Update()
 {
     if( !lvlChange )  // If we aren't changing the level at the moment ...
     {
         // ... check if we should
         if (Application.loadedLevelName == "Level1" && Score >= 280)
         {
             StartCoroutine(Intermission("Level2", 5.0f));
             Score = 0f;
         }
         else if //....
     }
 }

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

6 People are following this question.

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

Related Questions

How do I put a delay in this script ? 1 Answer

C# simple delay execution without coroutine? 2 Answers

Wait n-seconds before loading level 1 Answer

Score and Level Loading 1 Answer

LoadLevelAdditive delay 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