create audioSource via script?

i tryed this but not work , why

 private AudioSource m_audioSource;            

start()
{
m_audioSource = new AudioSource();
m_audioSource.loop = true;
}

and when it hit the “loop = true;” i get an error
i figure im not creating the AudioSource correctly .
the way im writing the code , it CANNOT be done via the inspector
so it has to be done via compleate script . any ideas how to do this ?

AudioSource audioSource = gameObject.AddComponent();
audioSource.clip = Resources.Load(name) as AudioClip;
audioSource.Play();

You need to have a clip assigned. In this example, I use Resources.Load(), but you could just use an assigned AudioClip or something like that.

Cheers :wink: