Hi all,
I have an array of 4 sounds added as an audioclip array:
public AudioClip[] sounds;
and I initialise it in Start() like so:
for (int i=0; i<4; i++)
{
sounds = new AudioClip*; *
Later on in my code I want to play a random sound from that bank. My object has an audiosource attached. The code I am using is:
audio.clip = sounds[Random.Range(0, sounds.Length)];
audio.Play();
The sounds are assigned in the inspector yet when my scene runs none of the sounds play.
What am I doing wrong?
Thanks in advance,
Martin
Your problem is here:
for (int i=0; i<4; i++)
{
sounds = new AudioClip*;*
}
Just kill this initialization. This is recreating the array four times, each time a little longer, but in the process it blows away the original clips you assigned in the inspector.
You might try a case statement for this idea. I remember having a lot of issues with audio when I was learning it. In any case, if you are using C# you might need to indicate the desired audio inside the parenthesis after your Play command.
audio.Play(walk);
or
audio.PlayOneShot(land);
anyone else feel free to “chime” it!?
Tha5 was just a little programming humor! 
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
/// <summary>
/// Class in charge to play sound in the game
/// </summary>
public class SoundManager : MonoBehaviour
{
public AudioSource GamePlaySound;
/// <summary>
/// Reference to the jump sound
/// </summary>
public AudioClip[] soundClips;
/// <summary>
/// Reference to the audiosource who will play the sounds
/// </summary>
AudioSource audioSource;
void Awake()
{
audioSource = GetComponent<AudioSource> ();
}
public void PlaySoundClip()
{
audioSource.PlayOneShot(soundClips[Random.Range(0,soundClips.Length)]);
;
}
}