Playing A Sound When Game Ends?
Hey everyone,
I'm VERY new to scripting and Unity and I'm working my way through some Brackey tutorials.
I'd like to music that plays when the game has ended. I have it setup so that the game currently ends when my box hits an object or falls off the world. This currently restarts the game and prints game ended to the console.
So the variable already exists that checks for a game end condition, but to be honest, I have no idea how to setup the reference for the sound.
I can think of the logic, I just don't understand the code.
Basically, if EndGame = true, than play gameOverSound.
How do I code this?
My gameManager.cs looks like this:
public bool gameHasEnded = false;
public float restartDelay = 1f;
public void EndGame ()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
Debug.Log("GAME OVER");
Invoke("restart", restartDelay);
}
}
void restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
I created a new game object called soundManager to somehow check if the game has ended and play a sound.
Answer by xxmariofer · Mar 15, 2019 at 08:39 AM
try this code, create a public audioclip and assign it in the inspector. take into account that the audio will stop when the new scene reeloads. put te line next to the Invoke.
AudioSource.PlayClipAtPoint(audioClip ,Camera.main.transform.position);
Your answer
Follow this Question
Related Questions
Hey all I am working on a 3D maze game. I would like to know how i could start and end the levels . 1 Answer
Using sound with the 3rd person starter asset 0 Answers
How do I make player interact with NPC 0 Answers
Can someone help me make a simple jump script? 6 Answers
pc linux mac standalone 0 Answers