Reputation: 14066
I have this code in OnStart
void Start () {
Camera.main.GetComponent<AudioListener> ().enabled = false;
GameObject sfx_go = new GameObject();
sfx_go.name = "sfx_go";
sfx_player = sfx_go.AddComponent<AudioSource> ();
sfx_go.AddComponent<AudioListener> ();
clip = (AudioClip)Resources.Load("Sounds/wind", typeof(AudioClip));
sfx_player.clip = clip;
}
then
void Update () {
if (Input.GetMouseButtonDown (0)) {
//TODO play sound
sfx_player.PlayOneShot(clip, 0.7F);
}
my file is a wav file and playing in unity , but not from code. what is the probleme here? It looks like the AudioClip is Nothing in the unity editor
thanks
Upvotes: 1
Views: 568
Reputation: 743
Your sound file needs to be in a folder called "Resources" within the assets folder. Currently, Unity is looking for "Assets/Resources/Sounds/wind" as a filepath for your file. If your file is simply in "Assets/Sounds/wind" then it is in the wrong place.
Upvotes: 2