• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Vcontrol · Mar 08, 2015 at 01:32 AM · unity 5gamesasteroids

How to add delay to the game without freezing all action

Hi everyone! I've been working with this Asteroids Clone for a few days now, and ran into some problem, mainly because I started learning C# like three weeks ago :D

Anyway, I have this script attatched to my bullet gameobject,that checks the collision with an bigger asteroid(I have two different kind of asteroids in this game...bigger one breaks into smaller ones if hit with a bullet, very basic stuff)

Also in this same script I have attached a Gamemanager and PointManager Objects, GameManager taking care of Starting, Resetting and Quitting Game(I install advancing to next level once I sort this currrent problem out) and Pointmanager counts points player gets when bullet destroys the asteroid in question.

To put things short ;

I have a counter(integer variable) which is linked to the number of asteroids on the current scene, and everytime player shoots(destroys) asteroid, this counter goes =-1 until it hits 0, when I call GameManager to reset the game.

And the problem(the most interesting(frustrating as f*) part is that once the counter I mentioned reaches 0 the GameManager Instantly calls resetGame(), which is not what I want ATM since my asteroids ,when exploded, bursts into these very nice (Or terrible, depenging who looks :D) particle effect clusterf* mess of neon colors XD...So I basically whish to find a way to play this explosion of final asteroid till the end , before calling the resetGame().

Tried Installing while loop, but It just freezes the game for time defined....

So heres a code snippet, if it is any help for you people out there.

Im terrible at commenting my code(excess of caffeine makes you work fast as hell :D) so If you got any question regarding this one, Im very much willing to give you insigths on whats happening on those lines...I made a few post-corrections in comment wise to make it easier to read just for you :)

Cheers --d

 using UnityEngine;
 using System.Collections;
 
 public class OnAsteroidCollision : MonoBehaviour
 {
     public GameObject GameManager; 
     public GameObject PointManager;
     //Points player gets form this particular asteroid   
     public int AsteroidValue = 20; 
     public GameObject[] smallAsteroids;
     public GameObject Explosion;
     private int explosionAmount;
     private int toHowManyPieces; 
    
 
     void Start()
     {
     explosionAmount = (int)Random.Range(12f, 24f);
     toHowManyPieces = (int)Random.Range(4f, 12f); 
     
     }
     void OnTriggerEnter2D(Collider2D other)
     {
 
         if (other.tag == "Bullet") 
         {
 
             for (int i = 0; i <= toHowManyPieces; i++) 
             {
                 int whichOne = Random.Range(0, smallAsteroids.Length);
                 Instantiate(smallAsteroids[whichOne], transform.position, Quaternion.identity);
             }
 
             for (int i = 0; i <= explosionAmount; i++)
             {
                 Instantiate(Explosion, transform.localPosition, Quaternion.identity);
 
 
             }
              DestroyObject(this.gameObject);
              DestroyObject(other.gameObject);
              PointManager.GetComponent<PointManager>().setPoints(AsteroidValue);
              PointManager.GetComponent<PointManager>().bigAsteroidHit();
            
             if (PointManager.GetComponent<PointManager>().getWinningCondition() <= 0) 
               {
                   //This is where the issue lies, I want this to happen , not instatly but within some range that shows the explosion of the final asteroid :D
                   
                   GameManager.GetComponent<GameManager>().resetGame(); 
             
               };
         }
 
          
          
     }
        
 }



Comment
Add comment · Show 2
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 AlwaysSunny · Mar 06, 2015 at 07:44 PM 0
Share

http://docs.unity3d.com/$$anonymous$$anual/Coroutines.html

 IEnumerator WaitThenReset( float delay ) {
   yield return new WaitForSeconds( delay );
   Game$$anonymous$$anager.resetGame();
 }
 
 //invoke with:
 StartCoroutine( WaitThenReset(2) );  
 //where 2 can be any value in seconds

Note you should only invoke this once, so...

 bool isWaiting = false;
 IEnumerator WaitThenReset( float delay ) {
    if (isWaiting) yield break;
    isWaiting = true;
    yield return new WaitForSeconds( delay );
    Game$$anonymous$$anager.resetGame();
    isWaiting = false;
 }


avatar image Vcontrol · Mar 06, 2015 at 08:35 PM 0
Share

I managed to solve this problem almost exactly the same way you mentioned trough coroutine.

Thanks anyway :)

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Hello, Im wondering if it's possible to create a 2.5D hybrid fighting game that is like smash bros and GGXrd combined? 2 Answers

Failed to Load Mono? 0 Answers

How to make an upgrading game? 1 Answer

Disabling and Enabling a collision 1 Answer

Space Shooter Asteroids Not Being Destroyed 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges