OK, so bear with me here, i am a complete newb to Unity and am just trying to find my way, so the first thing i wanted to try was to get a sound to play on a keypress.
I am not getting any erros, but my sound is not playing at all.
The file is located in a Resources folder.
Here is my code:
public class FirstPage : MonoBehaviour {
public AudioSource mySound;
// Use this for initialization
void Start () {
mySound = Resources.Load("05 - Listen to the Man_[plixid.com].mp3") as AudioSource;
}
void Update () {
if(Input.GetKeyUp(KeyCode.Space)){
mySound.Play();
};
}
}
i think the cast would be to AudioClip instead of to AudioSource
or mySound.clip = Resources.Load("music.mp3") as AudioClip;but why not just drag the clip to the source in the editor?
then do not need to attempt to load anything or worry about folders
AudioClips are the sound files, which you need to add to an AudioSource, which will play them.
So something like this. This wouldn’t work though, because you can’t load mp3 files with Resources.Load. You can use the WWW class to load mp3 files on mobile platforms, but otherwise you’ll need to decode mp3 files somehow.
public class FirstPage : MonoBehaviour
{
public AudioSource mySound;
// Use this for initialization
void Start()
{
AudioClip clip = (AudioClip)Resources.Load("05 - Listen to the Man_[plixid.com].mp3");
mySound.clip = clip;
}
void Update()
{
if (Input.GetKeyUp(KeyCode.Space))
{
mySound.Play();
};
}
}
Try removing the file extension on Resources.Load().
It can be a bit cantankerous, especially between build/editor. Its best to have simple names so you can rule out formatting problems that the compiler might have changed for some reason.