Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by Nano-Additcs · Sep 28, 2014 at 11:09 AM · audiosoundgame over

Play audio on game over GUI load or player death

Hi,

I would like to play audio when a player dies/when the game over GUI appears. I am currently using the audio.Play(); command however it plays repeatedly despite disabling audio loop on the actual audio clip.

Is there a way to play this audio clip once whilst leaving the command in the same location or is best practice to use another method?

Thank you for your assistance in advance. As a newb I'm banging my head against the learning curve on this one!

This is the C# script I am using:

using UnityEngine; using System.Collections;

public class GameManager : MonoBehaviour { // The score that the player currently has [HideInInspector] public int curScore; // The highest score the player has reached (saved) private int highscore; // Reference to our custom gui skin public GUISkin skin; // Values defining the width and height of our game over screen public Vector2 losePromptWH; // Boolean to check if we need to end the game or not [HideInInspector] public bool showGameOver;

 void Start () {
     // Grab the last saved highscore from the player prefs file
     highscore = PlayerPrefs.GetInt("Highscore");
 }

 void Update () {
     // If the player died and our current score is greater than our saved highscore
     if (showGameOver && curScore > highscore)
     {
         // Set the highscore to our current score
         highscore = curScore;
         // Now save the score as our new highscore
         PlayerPrefs.SetInt("Highscore", highscore);
     }
 }

 void OnGUI()
 {
     // Set the GUI's skin to our custom skin
     GUI.skin = skin;
     // Show our current score value at the top center of the screen 
     // (note: it uses the custom Score style in our skin)
     GUI.Label (new Rect(Screen.width/2 - 100,10f,200,200), curScore.ToString(), skin.GetStyle("Score"));
     // If the player died, show the game over screen
     if (showGameOver)
     {
         audio.Play();
         // Define the screen space for the game over window
         Rect currentGameOver = new Rect(Screen.width/2 - (losePromptWH.x/2), Screen.height/2 - (losePromptWH.y/2), losePromptWH.x, losePromptWH.y);
         // Generate a box based on the game over window rectangle
         GUI.Box (currentGameOver, "Game Over", skin.GetStyle("GAME OVER"));
         // Draw our current score within the game over window
         //GUI.Label(new Rect(currentGameOver.x + 15f, currentGameOver.y + 50f, currentGameOver.width * .5f, currentGameOver.height * .25f), "Score: " + curScore.ToString());
         // Draw our highscore within the game over window (if the highscore was beaten, it will show your current score)
         GUI.Label(new Rect(currentGameOver.x + 15f, currentGameOver.y + 70f, currentGameOver.width * .9f, currentGameOver.height * .35f), " Highest Score: " + highscore.ToString());
         // Draw a replay button on screen and check to see if it was clicked
         if (GUI.Button (new Rect(currentGameOver.x + (currentGameOver.width - 225), currentGameOver.y + (currentGameOver.height - 80), 130, 60), "", skin.GetStyle("Play")))
         {
             // If it is clicked, reload the level
             Application.LoadLevel("GamePlay");
             // Load the highscore from our save file
             highscore = PlayerPrefs.GetInt("Highscore");
         }

     }
 }

}

Comment
Add comment · Show 1
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 christoph_r · Sep 28, 2014 at 11:44 AM 0
Share

Have you tried placing a debug line before calling audio.Play();? If you get multiple of those, that's most likely where your issue is. $$anonymous$$eep in $$anonymous$$d that OnGUI is called multiple times per frame.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Nano-Additcs · Sep 29, 2014 at 11:08 AM

Thanks Christoph_r

Ended up adding two sound sources to the player and then specifying it under the game manager GUI

alt text

alt text

The script now looks like this:

public class MainCharacter : MonoBehaviour { // Value that controls the amount of force applied when clicking public float tapForce;

 // A reference to the map class

 //public Map map;

 // Reference to the manager class
 public GameManager manager;

 // Bool to check if we've died or not
 private bool isDead;

//Add audio source

 public AudioClip sound1;

 public AudioClip sound2;
 
 void Update () 
 {
     // Check to see if we're clicking and if we've not already died
     // (don't want to be able to move if we're dead)
     if (Input.GetMouseButtonDown(0) && !isDead && !(Camera.main.WorldToViewportPoint(transform.position).y > 1f))
     {
         // Add our tapForce to our bird's velocity if we do click
         rigidbody2D.velocity = new Vector2(0f, tapForce);
         // Control the rotation of the bird based on its velocity
         
         transform.rotation = Quaternion.RotateTowards(Quaternion.Euler(0f,0f,0f), Quaternion.Euler(0f,0f,90f), rigidbody2D.velocity.y);
     } else if (rigidbody2D.velocity.y < -.05) 
     {
         // Do the same here except only if it is falling
         transform.rotation = Quaternion.RotateTowards(Quaternion.Euler(0f,0f,0f), Quaternion.Euler(0f,0f, -90f), -rigidbody2D.velocity.y * 4f);
     }
 }
 
 // Check to see if we hit ANYTHING that's not a trigger
 void OnCollisionEnter2D(Collision2D other)
 
 {
     if(other.gameObject.tag == "BackGround")
     {
         return;
     }
     
             //Specify which audio clip to use and then play  
             audio.clip = sound1;
     audio.Play();
             // If we have, call the bird's die method
             Die ();
     //Destroy game object delayed in order to allow the sound enough time to play on die
     Destroy(gameObject, .15f);
 }

 // If we hit a trigger, we know it is a gate trigger
 void OnTriggerEnter2D(Collider2D other)

 {
     // In that case, add one to our player's score
     manager.curScore += 1;
     //map.Generate();
     other.collider2D.enabled = false;
             //Specify which audio clip to use and then play
             audio.clip = sound2;
     audio.Play();
     
 }
 
 // Method that controls the bird's death
 void Die()
 {
     // Set a boolean of isDead to true so that we can do some checks later
     isDead = true;
     // Force the Game Over window to pop up and generate our highscore
     manager.showGameOver = true;

 }

}

//end


screen shot 2014-09-29 at 9.06.22 pm.png (17.3 kB)
screen shot 2014-09-29 at 9.03.09 pm.png (31.0 kB)
Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

5.1 Channels is backwards in unity (non 3D) 0 Answers

Audio Settings Menu 2 Answers

play sound when moving stop sound when not 1 Answer

Why is my backtrack's volume getting lower? 0 Answers

3D Sound Max Distance Not Working. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges