Randomized Sound Delay Based on Minimum and Maximum Float Numbers

HI,

I’m trying to have a sound delay based on the the user’s input on minimum and maximum

Example Min = .4 and maximum = 3, I want to delay the sound in seconds using:

var soundDelayMin = .0f;
var soundDelayMax = .0f;

WaitForSeconds.Random.Range(soundDelayMin, soundDelayMax);
audio.PlayOneShot(sound);

but it isn’t working correctly I think I need another way of calculating the time for the floats. It has to be random between the two float numbers though.

Any ideas? I’ve been at this all day and have looked through every piece of documentation and still nothing…

I keep getting NullReferenceException: Object reference not set to an instance of an object

Thanks

yield WaitForSeconds(Random.Range(soundDelayMin, soundDelayMax));

–Eric

Thanks Eric for the quick response, but I’ve tried this and all I get is: Update() can not be a coroutine.

Is there something I have to set up in order to properly use a coroutine? My code is in the Update section so I need it to go there unless there’s another way.

Call another function to do it. But make sure the coroutine isn’t being called every frame.

There is also a delay property for AudioSource.play :

Thanks Petroz very helpful. How would I randomize the delay based on floats?

Also how do I assign a sound file to the audio.Play(44100);? I tried using a float but it didn’t work. How else can I assign it?

audio.Play(sound1,44100); ?