Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 StanClaas · Mar 28, 2016 at 12:28 AM · unity 5arrayaudiosoundarrays

How to play a random audio clip from an array in C#?

Hello!

So I've made an audio array like this:

public AudioClip[] shoot;

And placed two audio clips inside of it. Now my question is; how do I play a random audio clip from that array? All the methods I've seen online used obsolete code.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by Monkiman300 · Aug 08, 2016 at 12:54 PM

USING: Unity 5.3.5f1

My code works the same, but runs a random clip every 10 seconds. I figured this may be useful to someone at some point.

 using UnityEngine;
 using System.Collections;
 
 public class RandomSoundsScript : MonoBehaviour {
 
     public AudioSource randomSound;
 
     public AudioClip[] audioSources;
 
     // Use this for initialization
     void Start () {
 
         CallAudio ();
     }
 
 
     void CallAudio()
     {
         Invoke ("RandomSoundness", 10);
     }
 
     void RandomSoundness()
     {
         randomSound.clip = audioSources[Random.Range(0, audioSources.Length)];
         randomSound.Play ();
         CallAudio ();
     }
 }
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 timothywwcase · Dec 08, 2019 at 01:44 AM 0
Share

Thanks! This helped me make a simple music randomizer.

using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class $$anonymous$$usicRandomizer : $$anonymous$$onoBehaviour
 {
     public AudioClip[] audioClips;
     public AudioSource audioSource;
     public AudioListener audioListener;
 
     // Start is called before the first frame update
     void Start()
     {
         audioListener = GetComponent<AudioListener>();
         audioSource = gameObject.GetComponent<AudioSource>();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (!audioSource.isPlaying)
         {
             PlayRandom();
         }
     }
     void PlayRandom()
     {
         audioSource.clip = audioClips[Random.Range(0, audioClips.Length)];
         audioSource.Play();
        
     
     }
 
 
 
     
 }

avatar image
2

Answer by Fredex8 · Mar 28, 2016 at 10:26 AM

As an example, this will choose a random clip from the array and play it when you press space.

 private AudioSource audioSource;
 public AudioClip[] shoot;
 private AudioClip shootClip;


 void Start()
 {
     audioSource = gameObject.GetComponent<AudioSource>();
 }

 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Space))
     {
         int index = Random.Range(0, shoot.Length);
         shootClip = shoot[index];

         audioSource.clip = shootClip;
         audioSource.Play();
     }
 }

Random.Range is how you can get a random entry from any kind of array and of any length.

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
2

Answer by Miryum · Aug 22, 2021 at 11:36 AM

     public AudioClip[] sound;
     AudioSource audioSource;
 
      private void Start() {
         audioSource = this.GetComponent<AudioSource>();
     }
 
     public void PlaySound()
     {
         int rand = Random.Range(0, sound.Length);
         audioSource.clip = sound[rand];
         audioSource.Play();
     }

and call PlaySound() with GetComponent().PlaySound() from anywhere

Comment
Add comment · Show 2 · 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 zwcloud · Apr 30 at 03:49 PM 0
Share

It should be Random.Range(0, sound.Length), because the second parameter is exclusive.

avatar image Miryum · Apr 30 at 04:25 PM 1
Share

@zwcloud yes you are right, I didn't see that.

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

10 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

Related Questions

C# FFT and IFFT with Array Input 1 Answer

how to not repeat random array 1 Answer

Please help! Audio not working in another class.. 0 Answers

How can I get my sound array working here? 0 Answers

Array[] of children of a child 3 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