Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
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
Add comment · 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 : $$anonymous$$onoBehaviour
 {
     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

· Add your reply
  • Sort: 
avatar image
0

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
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

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

Related Questions

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

Soundarray for player is not working properly 1 Answer

Unity noob here, need help adding footstep logic 1 Answer

How to destroy object after it moves out of screen 7 Answers

[Solved] Destroy instantiated prefabs with a same tag, one by one, from last instantiated to first ? 2 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