How can I delay Loading "Win Screen"?

I added a particle system to show my ship being destroyed as well as a sound effect. The problem is, when the LevelManager is called, it immediately loads without showing my ship blowing up with the sound effect. How could I use the Invoke to delay this from happening? Or is there another way? Here is my code:

if(health <= 0f){
		Boom();
		Destroy (gameObject);
		AudioSource.PlayClipAtPoint(deathSound, transform.position);
		LevelManager man = GameObject.Find ("LevelManager").GetComponent<LevelManager>();
		man.LoadLevel("Win Screen");
		}
	if(health > 0f){
			AudioSource.PlayClipAtPoint(shieldHit, transform.position);
	}
}

Any help would be greatly appreciated!

You can either create an coroutine with an yield return new waitforseconds(x) that calls the changing method level after that within this same script, and call it when the ship gets destroyed. Or you can use an Invoke(method, time) too, but you will also have to extract the loadlevel call to other method

Thanks for the feedback. I’m still quit a newbie and tried using the “Invoke” many times with no success since we used it in the “EnemySpawner” script. After looking up the examples in Monoscript of coroutine I am still a bit confused on how to structure it in my code.

I’m all for experimenting as I have been doing but I am still stuck. Any chance you could give me an example of how coroutine would look for my code. Sorry I’m such a newbie!

Sure:

https://codeshare.io/5okEKb

if(health <= 0f){
	Boom();
	Destroy (gameObject);
	AudioSource.PlayClipAtPoint(deathSound, transform.position);
	StartCoroutine(loadLevelTimer());
	}
	if(health > 0f){
			AudioSource.PlayClipAtPoint(shieldHit, transform.position);
	}
}
IEnumerator loadLevelTimer()
{
    yield return new WaitForSeconds(3);
    LevelManager man = GameObject.Find ("LevelManager").GetComponent<LevelManager>();
    man.LoadLevel("Win Screen");
}

Haven’t tried to compile that, so you should double check the syntax

Thanks!!! I’ll try it out and tell you what happens!

1 Like

No problem, let me know if I can offer you further help :slight_smile:

Thanks again. It now shows my explosion with sound but the level manager is not loading the “Win Screen”. But this gets me out of my dazed loop and I am now checking deeper. I’ll let you know once I figure it out unless you beat me to the punch. :relaxed:

Here is what I have now:

if(health <= 0f){
Boom();
Destroy (gameObject);
AudioSource.PlayClipAtPoint(deathSound, transform.position);
StartCoroutine(loadLevelTimer());
}
if(health > 0f){
AudioSource.PlayClipAtPoint(shieldHit, transform.position);
}
}
IEnumerator loadLevelTimer()
{
yield return new WaitForSeconds(1);
LevelManager man = GameObject.Find (“LevelManager”).GetComponent();
man.LoadLevel(“Win Screen”);
}
void Boom(){
GameObject smokePuff = Instantiate (explosion, transform.position, Quaternion.identity) as GameObject;
smokePuff.particleSystem.startColor = gameObject.GetComponent().color;
}
}

is it returning any kind of error?

No, there are no errors in the console.

oh, this is happening because you are destroying the object, just move the Destroy method to the end of the ienumerator
(the way it is right now it is getting destroyed before having the chance to change the scene)

SUCESS!!! Thanks a million!

1 Like

Glad to know that you have it working as expected now :slight_smile:

Hi @Joao_Dalvi

[SUB QUESTION]

The Script you provided works fine. But i think the player would still be able to shoot even after the explosion gameobject is loaded in the screen. Which is weird because you should not be able to shoot after being destroyed.

is there a way to NOT invoke the Fire() (method which Instantiates the lasers) method after the explosion gameobject has been loaded.

Hello Steven, how are you?

Sure, just put a bool flag within the fire method:

If (!isDead){
Firemethod()
}

and then change this bool to true when the player start the dying behavior.

I’m fine.
Thanks, wow the solution was fairly simple. Why didn’t I think of that before.
I will try that and let you know…

OK, let me know if it works :slight_smile:

Was pretty busy today, sorry about the delay to answer

1 Like

It’s working perfectly.
Using the bool isDead private variable. I stopped the fire () method from running and also the stopped the players movement during explosion.

Sorry for late reply. It was late at night.
Thanks a bunch.

1 Like

Privacy & Terms