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 Script a Random Sound Player

John Moron
John Moron
1,658 Points

RandomSoundplayer wont load

I think my code looks correct is there something I'm doing wrong. My audio script won't load in unity, unity tells me to fix any errors but there aren't any.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class RandomPlayerEdi : 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> ();
    }

    // Update is called once per frame
    void Update () {

        // Increment a timer to count up to restarting.
        soundTimer = soundTimer + Time.deltaTime;

        // if time timer reaches the delay...
        if (soundTimer >= soundTimerDelay) {

            // ...reset the timer...
            soundTimer = 0f;

            //...choose a ramdom sound...
            AudioClip randomSound = soundClips[Random.Range (0, soundClips.Count)];

            //...and play the sound.
            audioSource.PlayOneShot (randomSound);

        }

    }
}
Pablo Rocha
Pablo Rocha
10,142 Points

Updated your question for readability :)

Cameron Button
Cameron Button
1,084 Points

I'm having this problem too, have you found a solution?

Jason Anders
Jason Anders
Treehouse Moderator 145,692 Points

Hey Cameron Button,

If you haven't had your issue resolved yet, please post your question in a separate thread. It's best to do this from the Video/challenge you first encounter the problem, by clicking the "Get Help" button and following the prompts. Make sure "include code" is checked.

This will ensure that the Community will be more able to assist you.

:dizzy:

Cameron Button
Cameron Button
1,084 Points

Jason Anders thank you, but I figured it out. I even left an answer down below! :)

2 Answers

Cameron Button
Cameron Button
1,084 Points

I think I have it figured out

The line....

"public class RandomPlayerEdi : MonoBehaviour"

should read...

"public class RandomSoundPlayer : MonoBehaviour"

John Moron
John Moron
1,658 Points

Ohh omg I would never have thought about that!

John Moron
John Moron
1,658 Points

It worked! Thank you so much! For your help! Now I can finally finish!!

John Moron
John Moron
1,658 Points

Hello Pablo sorry I thought I had my question in my post. I updated it please help me fix this I really need to move on.