• 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 /
  • Help Room /
avatar image
Question by simplicitydown · Jan 02, 2016 at 09:33 PM · c#audiodestroy

Cancelling audio before starting new audio

I almost have a working radio, where the player can walk up to it, press a button and cycle through songs. Everything works except for that with each button press the new song will play but the original will continue to play underneath it. For each new song that is played a new object is created but they are all called 'One Shot Audio,' so I don't know how to destroy them. If I can fix this bug then this should be a useful radio script for anyone who wants to use it. Here's the script attached to the player, thanks!:

 void OnTriggerEnter2D(Collider2D target){
         if (target.gameObject.tag == "radio") {
             radioEnter = true;
         }
 }
 void OnTriggerExit2D(Collider2D target){
         if (target.gameObject.tag == "radio") {
             radioEnter = false;
         }
     }
 
 public void radioUse(){
         if ((Input.GetKeyDown (KeyCode.M)) && song3on == true && radioEnter == true) {
             AudioSource.PlayClipAtPoint (song1, transform.position);
             song1on = true;
             song2on = false;
             song3on = false;
         }
         else if ((Input.GetKeyDown (KeyCode.M)) && song1on == true && radioEnter == true) {
             AudioSource.PlayClipAtPoint (song2, transform.position);
             song1on = false;
             song2on = true;
             song3on = false;
         }
         else if ((Input.GetKeyDown (KeyCode.M)) && song2on == true && radioEnter == true) {
             AudioSource.PlayClipAtPoint (song3, transform.position);
             song1on = false;
             song2on = false;
             song3on = true;
         }
     }
 public void Update(){
 raidoUse();
 }
Comment

People who like this

0 Show 2
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 simplicitydown · Jan 02, 2016 at 10:22 PM 0
Share

Just an update, don't know if this helps, but I got this response from StackO since no one was responding here, and it says this, but it gets as far as the AddComponent line, which immediately appears in red:

The following code is not tested.

 Public class Radio : MonoBehaviour
 {
     AudioSource output;
     public AudioClip[] songs;
     int songIndex = 0;
 
     void Start(){
         output = AddComponent<AudioSource>();
     }
 
     public void ToggleSong(){
         songIndex++;
         output.clip = songs[songIndex % songs.Length];
         output.Play();
     }
 
     public void TurnOn(){
         ToggleSong();
     }
 
     public void TurnOff(){
         output.Stop();
     }
avatar image hexagonius simplicitydown · Jan 03, 2016 at 09:26 PM 0
Share

fix the compile errors first and test it.

the first "public"needs to be lower case

at the end there's a braceright missing

1 Reply

  • Sort: 
avatar image

Answer by simplicitydown · Jan 04, 2016 at 08:31 PM

Here it is, working well in completion:

       public bool song1on = false, song2on = false, song3on = true;
      public bool radioEnter = false;
      
      AudioSource output;
      public AudioClip[] songs;
      int songIndex = 0;
      
      void OnTriggerEnter2D(Collider2D target){
      
      if (target.gameObject.tag == “radio”) {
      radioEnter = true;
      }
      }
      
          void OnTriggerStay2D(Collider2D target){
      
      if (target.gameObject.tag == “radio”) {
      radioEnter = true;
      }
      }
      void OnTriggerExit2D(Collider2D target){
      if (target.gameObject.tag == “radio”) {
      radioEnter = false;
      }
      }
      
      public void radioUse(){
      if ((Input.GetKeyDown (KeyCode.E)) && song3on == true && radioEnter == true) {
      TurnOn ();
      song1on = true;
      song2on = false;
      song3on = false;
      }
      else if ((Input.GetKeyDown (KeyCode.E)) && song1on == true && radioEnter == true) {
      TurnOff ();
      song1on = false;
      song2on = true;
      song3on = false;
      TurnOn();
      }
      else if ((Input.GetKeyDown (KeyCode.E)) && song2on == true && radioEnter == true) {
      TurnOff ();
      song1on = false;
      song2on = false;
      song3on = true;
      TurnOn ();
      }
      }
      
      private void Start()
      {
      output = gameObject.AddComponent<AudioSource>();
      }
      
      public void ToggleSong(){
      songIndex++;
      output.clip = songs[songIndex % songs.Length];
      output.Play();
      }
      
      public void TurnOn(){
 
 ToggleSong(); }
 
      public void TurnOff(){
      output.Stop();
      }
      
      private void Update()
      {
      radioUse ();
      }

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

57 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Audio doesn´t mute when i use my slider 0 Answers

How do I save the background music in the settings? 0 Answers

Trying to use audio causes errors when trying to use in an 'if' statement 1 Answer

Can I make this script to do with playing the right sound fx based on which player character is chosen more efficient? 1 Answer

C# float variable isnt behaving like its value 1 Answer


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