Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development How to Make a Video Game Game Audio Play Sounds with Scripts

My frog and bird aren't playing sound, all others are. Someone know why?

Here is my RandomSoundPlayer code:

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class RandomSoundPlayer : MonoBehaviour {

private AudioSource audioSource;
[SerializeField]
private List<AudioClip> soundClips = new List<AudioClip> ();
[SerializeField]
private float soundTimerDelay = 3f;
private float soundTimer;

// Use this for initialization
void Start () {
    audioSource = GetComponent<AudioSource> ();

}


void FixedUpdate () {
    soundTimer = soundTimer * Time.deltaTime;

    if(soundTimer >= soundTimerDelay) {
        soundTimer = 0f;

        AudioClip randomSound = soundClips[Random.Range (0, soundClips.Count - 1)];

        audioSource.PlayOneShot (randomSound);
    }
}

}

I create the Audio Source, did all the configuration in Unity but my frog and bird still don't make any sound. Bird Calls, Bird Footsteps and Player Footsteps aren't working. Did I miss something?

1 Answer

Daniel Hartin
Daniel Hartin
18,106 Points

Hi Mario

You have set the soundTimer to soundTimer multiplied (*) by Time.deltaTime which because soundTimer is 0 at the start always equates to zero. simply change this to

soundTimer = soundTimer + Time.deltaTime;

Hope this helps Daniel

Thanks Daniel! You're got it! I didn't notice that I was multiplying Time.delta by 0 always.