Last Block doesn't play sparkles animation

Hello, when i destroy the last block, the animation of the sparckles doesnt play and it goes inmediatly to the next scene, is there a way to delay this so it plays the animation of the sparckles and then it goes to the next scene?

Hi Omar,

It is possible to call a method with a delay using the Invoke method, equally, it is possible to get the duration of the particle effect.

In you SceneLoader.cs script you’ll already have a method like this;

public void LoadNextScene()
{
	int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;

	SceneManager.LoadScene(currentSceneIndex + 1);
}

What you could do is a another, something along these lines;

public void LoadNextScene(float delay)
{
    Invoke("LoadNextScene", delay);
}

In the above, you’ve created a new method which will take in a parameter of type float, named delay. We use the Invoke method to call the existing LoadNextScene method (without the parameter), as that already does what you want it to do, and we pass in the delay parameter so that the Invoke method will take that period of time before calling it.

e aware - the Invoke method takes the method name to execute as a string reference which are less than ideal (due to the ease in which you can spell the method name incorrectly).

In Level.cs the method which currently determines that the game is over once the last block is destroyed; is called BlockDestroyed, it makes a call to sceneLoader.LoadNextLevel, change that so that it calls the next method with the delay;

public void BlockDestroyed()
{
	breakableBlocks--;

	if(breakableBlocks <= 0)
	{
		sceneLoader.LoadNextScene(3f);    // change 3f to the duration of your particle effect
	}
}

In the above I have added 3f as an arbitrary value for the delay, the quickest and easiest approach to achieving what you want. Have a look at your particle effect and see how long its duration is, then use that value instead of the 3f.

A better approach to this would be to get a reference to the particle effect and then obtain the duration of the particle effect and pass that directly in. By doing so it would enable you to change the particle effect to be shorter or longer as you wish without the need to remember to change this hard-coded value.

I’ve not included the steps to do that as I didn’t want to put you off with a lengthy post full of changes, but if you want to make these changes too let me know and I can talk you through those steps in a further reply.

Hope this helps :slight_smile:


See also;

1 Like

It is the perfect solution, thanks a lot for your time Rob!

1 Like

You’re very welcome Omar, if you want the details for the other part, so that you can get the actual duration of the particle system, let me know :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms