- Home /
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();
};
}
}
}
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;
}
I managed to solve this problem almost exactly the same way you mentioned trough coroutine.
Thanks anyway :)
Your answer
Follow this Question
Related Questions
Script on multiple objects not working properly! 1 Answer
Space Shooter Asteroids Not Being Destroyed 3 Answers
Failed to Load Mono? 0 Answers
How to make an upgrading game? 1 Answer