get_audio can only be called from the main thread

I’m trying to make a game in which a sound would cyclically play in the background. Sort of like a metronome. It needn’t be really accurate though.

So I wrote the following code for a MusicBoxObject (this obviously isn’t all the functionality I need, but I like to go in small steps)

var speed = 120;

var clip : AudioClip;
private var delay = 0;

function Start () {
	delay = 60/speed;

	var thread = System.Threading.Thread(doLogic);
	thread.Start(); 	
}


function doLogic(){
	audio.PlayOneShot(clip);
	while(true){
		System.Threading.Thread.Sleep(60/speed);
		audio.PlayOneShot(clip);
	}
}

And well… I got the error I wrote in the title. Please bear with me if what I’ve done is stupid, but I’ve only started fiddling in Unity a few days ago.

Could anyone tell me, how can I get this simple functionality going? I would appreciate if someone explained why I can’t play a sound from a separate thread and suggested a better solution, because I’m pretty sure this is entirely doable in Unity

Use a Coroutine.