- Home /
Make the audio manager select from an array of sounds
I have two scripts that handle sounds. One is an AudioManager that should play the sounds I want, Another is a"Sounds" class which handles each sound for itself.
It works great with one AudioClip, However, I'd like to make the AudioClips in the Sounds class as Arrays(or lists?) and I am having trouble making it work. I get an index out of range.
Here is the Sounds class:
using System.Collections.Generic;
using UnityEngine.Audio;
using UnityEngine;
[System.Serializable]
public class Sounds
{
public string name;
public AudioClip[] audioClip;
[Range(0f, 1f)]
public float volume;
[Range(0f, 1f)]
public float pitch;
public bool loop;
public AudioMixerGroup audioMixerGroup;
[HideInInspector]
public AudioSource audioSource;
}
here is the AudioManager Script:
using System;
using System.Collections.Generic;
using UnityEngine.Audio;
using UnityEngine;
using Random = System.Random;
public class AudioManagerNew : MonoBehaviour
{
public Sounds[] sounds;
public static AudioManagerNew instance;
private void Awake()
{
DontDestroyOnLoad(gameObject);
if (instance == null)
{
instance = this;
}
else
{
Destroy(gameObject);
return;
}
foreach (var s in sounds)
{
s.audioSource = gameObject.AddComponent<AudioSource>();
s.audioSource.clip = s.audioClip[UnityEngine.Random.Range(0, s.audioClip.Length)];
s.audioSource.volume = s.volume;
s.audioSource.pitch = s.pitch;
s.audioSource.outputAudioMixerGroup = s.audioMixerGroup;
s.audioSource.loop = s.loop;
}
}
public void PlaySound(string name)
{
Sounds s = Array.Find(sounds, sound => sound.name == name);
s.audioSource.Play();
if (s == null)
{
Debug.LogWarning("Sound" + name + "not found");
return;
}
}
//How to play sounds from other scripts:
// FindObjectOfType<AudioManager>().Play("string of clip name");
}
I'm clearly not writing the array the correct way, how can I make the audio manager recognize the AudioClip array?
where are you getting the index out of range? are you sure you have drag and drop all the audio clips into the sound class and all the sounds in themanager class?
Answer by Rorrors · Dec 02, 2020 at 05:35 PM
Just a easy script to test. Put it on a gameobject1, give up the ammount of tracks you want to play. Then create gameobjects for the soundtracks, add a compoment "audio source". drag you audio track in it. Turn off, play on awake. Then drag those soundtracks, into the gameobject with the script.
public AudioSource soundtracks;
public AudioSource[] soundtracksArray;
void Update()
{
if (!soundtracks.isPlaying)
{
PlayAudioTracksRandom();
}
}
}
void PlayAudioTracksRandom()
{
if (!soundtracks.isPlaying)
{
soundtracks = soundtracksArray[Random.Range(0, soundtracksArray.Length)];
soundtracks.Play();
soundtracks.PlayDelayed(44100);
}
}
Your answer
Follow this Question
Related Questions
Play sound from an array, not random, and only once 3 Answers
How Do you stop audio of another app? 0 Answers
How many audio clip arrays are supported? 1 Answer
Forward and Backward 3D sound? 1 Answer