Lag Problem.

Im creating a 2d game. I wanted to make a script which would choose random amount of time and would choose random sound. And it lags when i use this script. Can this be because of too many Random.Range commands ?


var CanGroan : boolean;
var Groan1 : AudioClip;
var Groan2 : AudioClip;
var Groan3 : AudioClip;

function Start () {


function Update () {
	if(CanGroan == true) {
		var SoundSel : int = Random.Range(1,3);
		if(SoundSel ==1) {
		if(SoundSel ==2){
		CanGroan = false;

function Groan() {
	var WaitGroan : int = Random.Range(10,19);
	yield WaitForSeconds(WaitGroan);
	CanGroan = true;
function WaitFirstt() {
	var WaitFirst : int = Random.Range (7,15);
	yield WaitForSeconds (WaitFirst);
	CanGroan = true;

I would suspect the yield call or PlayOneShot(). Check that your AudioClip is not configured to be streamed, and try a smaller AudioClip.

If yield is the problem then get rid of it and instead set a variable for what time you want to groan next then check the current Time.time against the next groan time in Update().