Question by
StefanWullems · Jun 19, 2017 at 01:35 PM ·
audioaudiosourcemusic
Getting music to play
Hey,
I want to play some music in my game but I can't get it to play, I already attached the script to a game object that I placed inside the scene. this is my code:
static MusicPlayer instance = null;
void Awake()
{
if (instance != null)
{
Destroy(gameObject);
}
else
{
instance = this;
GetComponent<AudioSource>().Play();
DontDestroyOnLoad(gameObject);
}
}
Thanks in advance.
Comment
Answer by Orion_78 · Jun 19, 2017 at 02:23 PM
You need to attach an audiosource next to your script
If this is not your issue here, maybe try
AudioSource audioSourceMusic;
void Start()
{
audioSourceMusic = gameObject.AddComponent<AudioSource>();
}
void PlayMusic(AudioClip clip)
{
audioSourceMusic.clip = clip;
audioSourceMusic.Play();
}
or at last https://docs.unity3d.com/ScriptReference/AudioSource.PlayClipAtPoint.html
Your answer
Follow this Question
Related Questions
Audio Clipping Problem 0 Answers
GetSpectrumData from an audio source when Volume is 0? 0 Answers
Audio is way too soft on mobile but alright in Unity Editor! 0 Answers
Audio Cutting Out Unexplainably 1 Answer