Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Calling a random sound from sound array- no sound being called.

Discussion in 'Scripting' started by cristo, Aug 31, 2019.

  1. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    I have a gameObject that has has four audio sources. I can't call the audio sources from this code. Which is also attached to the gameObject. All the audio clips have been assigned in the inspector, and if I tick "Play on awake" inside the audio source in the inspector, sounds are made.
    I'm not sure what is wrong. Any insight would be great.

    Code (CSharp):
    1. public class SoundArrays : MonoBehaviour
    2. {
    3.     public AudioClip[] AudioClipArray;
    4.     private AudioSource Sound_Array;
    5.  
    6.     // Use this for initialization
    7.     void Start()
    8.     {
    9.    
    10.         Sound_Array = GetComponent<AudioSource>();
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.    
    17.             Sound_Array.clip = AudioClipArray[Random.Range(0, AudioClipArray.Length)];
    18.             Sound_Array.Play();
    19.      
    20.     }
    21.  
    22. }
     
  2. ibbybn

    ibbybn

    Joined:
    Jan 6, 2017
    Posts:
    193
    They are being played but you start a new clip every frame.

    Also what do you mean with 4 audiosources? Do you mean you have 4 audioclips in that cliparray?
    You're caching only 1 audiosource right here and playing only 1 frame of each clip.
     
    Dextozz likes this.
  3. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    183
    Take all that code out of update, then call that code from a key press or other trigger.
     
    Dextozz likes this.
  4. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Thanks for the feedback people. Yes I put an audio source on the gameObject for each audio clip. By your statement ibbybn I'm guessing I need only one audiosource despite the number of clips the audio source can play.
     
  5. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    183
    Yes, you only need one AudioSource if you plan to assign the clips as you have done in line 17.

    Another option would be to have 4 audiosources, each with a seperate clip, assign them to a public audio source array in the inspector, then choose an audiosource at random and play it.
     
    cristo likes this.
  6. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Thanks for the feedback.
     
  7. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    You're essentially playing a new random clip every frame here. As @EdGunther suggested, create an input system that will play a random sound when requested by the user.


    Code (CSharp):
    1. private void Update()
    2. {
    3.    if(Input.GetKeyDown(KeyCode.Space))
    4.    {
    5.       Sound_Array.clip = AudioClipArray[Random.Range(0, AudioClipArray.Length];
    6.       Sound_Array.Play();
    7.    }
    8. }
     
    cristo likes this.