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 /
avatar image
0
Question by Hydroid · Dec 13, 2016 at 03:22 AM · buttonaudiosourceaudioclip

Play a random audio clip when button is clicked

I could not find any forum post related to my problem. I have a script that has no errors, yet, when I click the button to play a random sound, nothing happens. I have the script hooked up to an empty game object and I have the script and audio sources all hooked up there. I am using the right function in the OnClick() for the button but I'm confused on why nothing happens.

 public class PlaySound : MonoBehaviour {
     public AudioClip[] clips;
     
     public void LetsPlay()
     {
         int clipPick = Random.Range(0, clips.Length);
         GetComponent<AudioSource>().clip = clips[clipPick];
         GetComponent<AudioSource>().Play();
     }
     
 }

Thank you in advance.

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 getyour411 · Dec 13, 2016 at 03:24 AM 1
Share

Do you have an active audio listener near the PlaySound object if you have it configured that way? Add a debug.log into LetsPlay to verify it is being executed.

avatar image Hydroid getyour411 · Dec 13, 2016 at 09:11 PM 0
Share

Okay so I took your advice and It stopped at GetComponent().clip = clips[clipPick];

$$anonymous$$issingComponentException: There is no 'AudioSource' attached to the "Soundmanager" game object, but a script is trying to access it. You probably need to add a AudioSource to the game object "Soundmanager". Or your script needs to check if the component is attached before using it.

That's the error I get. In the soundmanager, which is an empty game object, I have all of the sounds put into a list. Every time I add an audio listener to the Soundmanager, it tells me there can only be one audio listener.

3 Replies

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

Answer by Hydroid · Dec 15, 2016 at 04:21 AM

Okay so I fixed it. Apparently, I wasn't supposed to put anything into the AudioSource once i put it into the Soundmanager.

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

Answer by salex100m · Dec 14, 2016 at 02:13 AM

Does this work? I guess you can do the same thing with clips, but i would use AudioSource

 void Awake () {
     AudioSource[] audios = gameObject.GetComponents<AudioSource>();
 }
 
  public void LetsPlay(){
           int clipPick = Random.Range(0, audios .length);
           audios [clipPick].play();
 }


Comment
Add comment · Show 3 · 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 Hydroid · Dec 14, 2016 at 03:51 AM 0
Share

Thank you! I had to fix some errors, of course. But Here is what I got.

    AudioSource[] audios = gameObject.GetComponents<AudioSource>();
     
 
     public void LetsPlay()
     {
         int clipPick = Random.Range(0, audios.Length);
         audios[clipPick].Play();
     }

The only error I get is "A field initializer cannot reference the non-static field, method, or property 'Component.gameObject'

avatar image fafase Hydroid · Dec 14, 2016 at 05:38 AM 0
Share

Your method is most likely static. $$anonymous$$ake it non-static by removing the static keyword.

avatar image Hydroid fafase · Dec 14, 2016 at 09:39 PM 0
Share

I checked the script; there is nothing static. Any other ideas? And thank you for trying to help me so far

avatar image
0

Answer by jkaniuk · Dec 15, 2016 at 03:00 PM

Hi @Hydroid, I see you have solved you problem, however I went ahead and wrote a quick script which you can compare to. I'm just wondering why you have an array of AudioSources rather than an array of AudioClips? Hope this helps

 public class Player1 : MonoBehaviour
 {
     private AudioSource a_source;
     public AudioClip[] a_clips;
 
 void Start ()
         {        
         a_source = gameObject.AddComponent<AudioSource>();      
         //can also switch add to get if object already has an audio source
         }    
     
 void Update ()
          {
         if(Input.GetMouseButtonDown(0))        
             PlayRandomSound();            
          }
 
 public void PlayRandomSound()
         {
         int selection = Random.Range(0, a_clips.Length);
         a_source.PlayOneShot(a_clips[selection]);
         }
 }


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

6 People are following this question.

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

Related Questions

how to stop an audio source from playing multiple times; change button text 0 Answers

Does enabling and disabling an AudioSource cause the sound to play, if playOnAwake is enabled? 1 Answer

Playing an audio clip 4 Answers

Using yield WaitForSeconds after AudioSource.PlayClipAtPoint 1 Answer

A better script for a machine gun? 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