- Home /
How to make AudioSource play defined clips using String?
The question is complicated but what I want to do is basically this. I have defined my audioclips
public AudioClip engine1;
public AudioClip engine2;
public AudioClip engine3;
Now I have my create audiosource
engSource = CreateAudioSource (engineSoundBank, true, true, enginePositionV);
and I defined a string
public string engineSoundBank = null;
BASICCALLY, what I am trying to do is have my constant audio source be able to change sound clips eg
engine1 is playing, then engineSoundBank gets set to engine2, now engine2 is playing, then engine3, etc etc. I'm sure this is the wrong way to do this but does anyone know of an alternate way to do this OR make my method work? Thanks.
Answer by robertbu · Jul 18, 2014 at 03:53 AM
If you want to play them one after another, then you can put them in an array and then walk true array:
public AudioClip[] engines;
Then in the Inspector, you can open the array, set the size, and drag the clips into the array.
Then you keep an integer called something like curClip. When you want to advance to the next clip, you would do:
curClip = (curClip + 1) % engines.Length;
Then you would pass an audio clip to your CreateAudioSource() method using engines[curClip].
Thank you! I manged to code something successful with smooth blending.
Your answer
Follow this Question
Related Questions
Car Audio c# script not working 3 Answers
Car engine sound 1 Answer
How to make a car not slow down while turning? 2 Answers
Play Audio OnTriggerEnter then loop 2 Answers
Unity WebGL audio not looping when started with playdelayed() 1 Answer