- Home /
Audio clip change via script,changing audio clip in audio source via script
Hello, So I have this AI script that has different states in it. i have an audio source located on my character an i wold like the audio clip to change in every state. whats the approach to go about that? also, I have very limited scripting knowledge. thanks in advance love you all
Answer by rh_galaxy · Apr 30 at 08:45 PM
What's important is what audio you want to play. You can use a general AudioSource to start playing a sound that is mixed with other sounds. Add more audio sources for sounds that are continuous and require start/stop playback.
public AudioClip clipFire; //set in inspector to a clip
AudioSource generalAS;
AudioSource engineAS;
void Start()
{
//this:
generalAS = GetComponent<AudioSource>();
//or this for multiple audio sources:
foreach (AudioSource aSource in GetComponents<AudioSource>())
{ //we have 2 audiosources
if (aSource.clip!=null && aSource.clip.name.Equals("engine"))
engineAS = aSource;
else
generalAS = aSource;
}
}
void Update()
{
if(bulletFire) generalAS.PlayOneShot(clipFire);
if(startEngine) engineAS.Play();
if(stopEngine) engineAS.Stop();
}
Your answer
Follow this Question
Related Questions
Dynamic Backgroundmusic 1 Answer
AI Pathfinding script with Speed Adjustment 1 Answer
Hello eveyone, i want to make a footstep sound but my code doesn't make the audio work properly 2 Answers
It is possible to start a streaming AudioClip not from the beginning ? 1 Answer
Audio loops too early 2 Answers