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 EvyBenita · Jul 09, 2016 at 07:00 PM · audiosourceaudioclipobject pool

Is there a way to update the audioclip of an object pooled audiosource instance?

Hello,

I have an object pool of audiosource instances that I'd like to change the default audioclip of during runtime. I'm a complete noob to coding, sorry if the question is very basic :)

I've got this in the Start method. Is there maybe something I can add to the Update function to change the default audioclip of the instantiated objects?

 void Start () {
 
     pooledAudios = new List<AudioSource> ();

     for (int i = 0; i < pooledAmount; i++) {
         AudioSource audio = (AudioSource)Instantiate (pooledAudioSource);
         audio.gameObject.SetActive (false);
         pooledAudios.Add (audio);
     }
         
 }

HUGE thanks to anyone who can help! :)

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by EvyBenita · Jul 09, 2016 at 07:03 PM

Sorry, this is my full ObjectPooler script:

public class ObjectPooler : MonoBehaviour {

 public AudioSource pooledAudioSource;
 public int pooledAmount = 10;
 public bool willGrow = true; //lets the audiosource list grow if it's needed.

 List<AudioSource> pooledAudios;

     
 void Start () {
 
     pooledAudios = new List<AudioSource> ();

     for (int i = 0; i < pooledAmount; i++) {
         AudioSource audio = (AudioSource)Instantiate (pooledAudioSource);
         audio.gameObject.SetActive (false);
         pooledAudios.Add (audio);
     }
         
 }

 void Update(){


 }
             

 public AudioSource GetPooledAudio(){
 
     for(int i = 0; i < pooledAudios.Count; i++){

         if(!pooledAudios[i].gameObject.activeInHierarchy){
             return pooledAudios[i];
         }

     }

     if (willGrow) {
     
         AudioSource audio = (AudioSource)Instantiate (pooledAudioSource);
         pooledAudios.Add (audio);
         return audio;
     }

     return null;
 }


}

And this my SoundSwap script, which works fine on non-list audio:

 public AudioSource zone;

 public AudioClip clip1;

 void OnTriggerEnter2D(Collider2D other){

     if (other.gameObject.tag == "player") {

// Debug.Log ("audio clip swapped"); zone.clip = clip1;

         }
     }
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 EvyBenita · Jul 10, 2016 at 10:55 AM 0
Share

Got it working in the end. Ins$$anonymous$$d of changing the instances themselves, I'm changing the clip in the coroutines where I'm calling them, and a public static variable as an identifier of which clip to play. There's probably a better way of doing it, but at least it works :)

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

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

Related Questions

Newbie with OnTriggerEnter & Audio Files 0 Answers

I want the audio to play when the player nears the object, but fade away when they walk away. 0 Answers

Audio won't play on collision 0 Answers

Determine end of audio clip if paused and resumed inbetween? 1 Answer

Object Reference not set to an instance of an object 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