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 Rxanadu · Jul 17, 2013 at 01:04 PM · audioplaycoroutinesplayoneshot

Making a song play once when using bool variables

I'm having some issues with playing music when my game ends. Whenever the music starts playing, it starts over, causing the song to play continuously. Currently, I've set up my script so the music starts playing if the audio is not playing. However, I'm not sure how to check when the song has ended without resorting to the use of coroutines, which don't work as well for how my script is set up currently. I've also tried using the PlayOneShot() function, but it has a similar result to before, except the song restarts much faster than just using the Play() function (making the music sound like it's stuttering rapidly)

Here is the script I'm using to handle the game over music:

 public class GameOverController : MonoBehaviour
 {
     public bool gameEnded = false;
     public AudioClip gameOverMusic;
 
     GameObject player;
     bool gameRestarting = false;
     int playerScore;
     bool songPlaying = false;
 
     void Start()
     {
         GetComponent<GUITexture>().guiTexture.pixelInset = new Rect(0, 0, Screen.width, Screen.height);
         GetComponent<GUITexture>().guiTexture.enabled = false;
         GetComponent<GUIText>().enabled = false;
 
         player = GameObject.FindGameObjectWithTag(Tags.player);
     }
 
     void Update()
     {
         Debug.Log("Game ended: " + gameEnded);
         playerScore = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<PlayerScore>().score;
         if (gameEnded)
         {
             Destroy(player);
             SetToHalfColored();
             songPlaying = true;
             GameObject.Find("object-emitters").GetComponent<RandomSpawner>().enabled = false;
             GetComponent<GUIText>().enabled = true;
             GetComponent<GUIText>().text = "Final Score: " + playerScore;
         }
 
         if (songPlaying) {
             PlayGameOverMusic();
             //audio.Stop();
         }
 
         if (gameRestarting)
         {
             gameEnded = false;
             GetComponent<GUIText>().enabled = false;
             GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<SceneFadeInOut>().EndScene();
         }
     }
 
     void OnGUI()
     {
         if (gameEnded)
         {
             if (GUI.Button(new Rect((Screen.width / 2), (Screen.height / 2) - 70 / 2, 100, 30), "Replay"))
             {
                 gameRestarting = true;
             }
         }
     }
 
     void SetToHalfColored()
     {
         GetComponent<GUITexture>().guiTexture.color =
             new Color(GetComponent<GUITexture>().guiTexture.color.r,
                 GetComponent<GUITexture>().guiTexture.color.g,
                 GetComponent<GUITexture>().guiTexture.color.b,
                 .3f);
     }
 
     void PlayGameOverMusic() {
         if (!audio.isPlaying)
         {
             audio.clip = gameOverMusic;
             audio.Play();
         }
     }
 }

If there is anyone who knows of a proper way to play the music only once, please let me know.

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 dorpeleg · Jul 17, 2013 at 02:02 PM 0
Share

Change the Play() to PlayOneShot().

Then try adding songPlaying = false after the PlayOneShot().

1 Reply

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

Answer by PAEvenson · Jul 17, 2013 at 01:59 PM

Your issue is here:

 if (songPlaying) {
             PlayGameOverMusic();
             //audio.Stop();
         }

You can add a bool when you start playing the audio file and check against it in the Update function...There is quite of few ways you can handle this. Here is probably the easiest way to fix your problem.

 private bool mhasPlayedGameOverMusic = false; //Add this to your class variables
 
 void Update()
 {
 //....
 
 if (songPlaying && mhasPlayedGameOverMusic == false) {
             PlayGameOverMusic();
             //audio.Stop();
         }
 
 }
 
 void PlayGameOverMusic() {
         if (!audio.isPlaying)
         {
             mhasPlayedGameOverMusic = true;
             audio.clip = gameOverMusic;
             audio.Play();
         }
     }
 
 
 
 
Comment
Add comment · Show 1 · 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
avatar image Rxanadu · Jul 17, 2013 at 03:52 PM 0
Share

It worked! I had a feeling I needed another bool variable somewhere for this to work, specifically one that stopped the PlayGameOver$$anonymous$$usic functions from running continuously during the gameEnded bool 'state' I created.

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

17 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

Related Questions

How to string multiple audio clips together 1 Answer

Randomly play audio 0 Answers

AudioSource plays noisy audio 1 Answer

Some audio does not play on Windows build but works perfectly in editor and on Mac 1 Answer

audio.Play() not always playing the entire AudioClip 5 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