- Home /
How to create an audio manager script with input from triggers?
I have got five triggers in my scene.
On entering each trigger I would like to play a particular music. When a new audio starts playing I want the previous one to be stopped.
I want to create one script for controlling all the audio so that it reduces the memory usage.
I am not able to understand how I can get access to the various variables required for this.
Answer by sethuraj · Jul 23, 2014 at 11:00 AM
Before getting into the solution,try to convert your logic to code and at least try to show your progress in code so that we can help you. Ok for now you can implement something like this.Create a soundmanager script which will hold all sound datas and to which you will make calls to play each sound
This is the sound manager script "SoundManager.cs" use it on an empty gameObject
//The AudioSource to which we play any clips
private AudioSource A_Source;
//The audioclips which you should assign through inspector
public AudioClip Clip_00;
public AudioClip Clip_01;
public AudioClip Clip_02;
public AudioClip Clip_03;
public AudioClip Clip_04;
//Singleton accessor
public static SoundManager Instance;
void Awake()
{Instance=this;}
void Start()
{
//Add the audio source
A_Source= gameObject.AddComponent<AudioSource>();
}
public void PlaySoundTrack(int TrackID)
{
//Stop any playing music
A_Source.Stop();
switch(TrackID)
{
case 1:
A_Source.PlayOneShot(Clip_01);
break;
case 2:
A_Source.PlayOneShot(Clip_02);
break;
case 3:
A_Source.PlayOneShot(Clip_03);
break;
case 4:
A_Source.PlayOneShot(Clip_04);
break;
default:
A_Source.PlayOneShot(Clip_00);
break;
}
}
In your five trigger script's OnTriggerEnter functions use like this
public int ID_Collider=0;
void OnTriggerEnter(Collider Target)
{
if(Target.gameObject.Tag=="Player")
{
//Since the sound manager class name is SoundManager.cs
SoundManager.Instance.PlaySoundTrack(ID_Collider);
}
}
Answer by dinuraimesh · Jan 30, 2019 at 03:50 AM
The post is old. But guess this would be helpful for everyone. This audio manager gives control over every every audio clip with effects.
Your answer
Follow this Question
Related Questions
Problem with playing sound on Trigger-Enter 2 Answers
One sound gets quieter when other is played? 1 Answer
play audio from frequency and amplitude file 0 Answers
Getting audio to play on collision 3 Answers