Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.