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
1
Question by GregXavier · Apr 13, 2013 at 03:33 AM · randomsoundaudiosourceaudioclip

Only able to play a single sound through AudioSource.

I am trying to set up my script so that it will choose randomly from three sounds when I fire the weapon. It works if I set it up with one sound but fails to play anything when I put in my if statement to attempt to randomise the sound. I've been poring over the manual entries on AudioSource and AudioClip all morning and have not been able to figure this out.

 public AudioClip launchMissile1;//audio for firing missile
 public AudioClip launchMissile2;//audio for firing missile
 public AudioClip launchMissile3;//audio for firing missile
 
         //capture keyboard input to launch a starbase missile
         if(Input.GetKeyDown(keyFire))
         {
             if(weaponLoaded)
             {
                 FireWeapon();
                 float randSound = Random.value;
                 if(randSound < 0.33)
                 {
                     print ("sound1");
                     audio.clip = launchMissile1;
                     audio.Play();
                 }
                 if(randSound >= 0.33 && randSound <= 0.66)
                 {
                     print ("sound2");
                     audio.clip = launchMissile2;
                     audio.Play();
                 }
                 if(randSound > 0.66)
                 {
                     print ("sound3");
                     audio.clip = launchMissile3;
                     audio.Play();
                 }
                 rigidbody.AddRelativeForce(Vector3.up * recoil);
             }
             else
                 print ("We're still loading the launcher!"); //debugging info            
         }

So the question at it's most basic is "How do I change the clip that my audiosource is playing?".

Thanks in advance for any assistance.

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 Loius · Apr 13, 2013 at 04:43 AM 0
Share

Is it printing the debugs? If not, trying using Debug.Log ins$$anonymous$$d of print, i once had print not work. Or was blind. :)

I'm pretty sure I've done this exact thing in one of my projects...

avatar image GregXavier · Apr 13, 2013 at 05:07 AM 0
Share

Ah my apologies, I did mean to mention that! Yes it is printing the debug text. Sound1, sound2, sound3 etc.

1 Reply

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

Answer by Khada · Apr 17, 2013 at 08:04 AM

This works for me (object must have attached AudioSource):

     public AudioClip clip1; //assigned via inspector
     public AudioClip clip2; //assigned via inspector
     public AudioClip clip3; //assigned via inspector
     
     void Update () 
     {
         if(Input.GetKeyDown(KeyCode.Space))
         {
             int rand = Random.Range(0,3);
             
             switch(rand)
             {
                 case 0: audio.clip = clip1; break;
                 case 1: audio.clip = clip2; break;
                 case 2: audio.clip = clip3; break;
             }
             
             if(audio.clip != null)
                 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 GregXavier · Apr 17, 2013 at 08:25 AM 1
Share

100% successful. Thanks $$anonymous$$hada!

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

12 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

Related Questions

How to get decibel's value of game's sound? 0 Answers

Audio cuts out once a certain number of sounds are played 0 Answers

PlayOneShot returns false for isPlaying 5 Answers

Play AudioSource Backwards 2 Answers

Almost any object can play a sound. They all need an Audio Source? 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