Problem with sputtering audio

Newbie question:

I am working on a space game where the character is a spaceship. When the player presses the accelerator button, the ship accelerates, fire and smoke come out of the engine, and an audio file is played for the engine sound.

Although, I am guessing there is a better way to do this, I enable/disable the engine sound by using GetComponent(AudioSource).enable, as shown below. This seems to be causing a really bad sputtering of the audio file. I’m not sure if it is constantly restarting the file or its playback is just really choppy.

Does anyone know why this is occurring, how to fix, or a better way to get the intended effect? Thanks.

(updated code below based on meat5000’s feedback → did not solve issue)

var spaceFighter : GameObject;
var engineFire1 : GameObject; 
var engineFire2 : GameObject; 
var engineGlow1 : GameObject; 
var engineGlow2 : GameObject;
var engineLightSource1 : GameObject; 
var engineLightSource2 : GameObject;
var engineSmoke1 : GameObject; 
var engineSmoke2 : GameObject;
var engineSparks1 : GameObject; 
var engineSparks2 : GameObject;
var engineSounds : GameObject; 

var forwardForce = 1000;
private var drag = 2;

function Start () {


function OnGUI() {

		if (GUI.RepeatButton(Rect(Screen.width*0.88,Screen.width*0.48,Screen.width*0.1,Screen.width*0.1),"Accelerate")){
			spaceFighter.rigidbody.AddRelativeForce(Vector3.forward * forwardForce);
				engineFire1.GetComponent(ParticleRenderer).enabled = true;	
				engineFire2.GetComponent(ParticleRenderer).enabled = true;	
				engineGlow1.GetComponent(ParticleRenderer).enabled = true;	
				engineGlow2.GetComponent(ParticleRenderer).enabled = true;
				engineLightSource1.GetComponent(Light).enabled = true;	
				engineLightSource2.GetComponent(Light).enabled = true;
				engineSmoke1.GetComponent(ParticleRenderer).enabled = true;	
				engineSmoke2.GetComponent(ParticleRenderer).enabled = true;		
				engineSparks1.GetComponent(ParticleRenderer).enabled = true;	
				engineSparks2.GetComponent(ParticleRenderer).enabled = true;
				engineSounds.GetComponent(AudioSource).enabled = true;	
	            engineFire1.GetComponent(ParticleRenderer).enabled = false;	
				engineFire2.GetComponent(ParticleRenderer).enabled = false;	
				engineGlow1.GetComponent(ParticleRenderer).enabled = false;	
				engineGlow2.GetComponent(ParticleRenderer).enabled = false;
				engineLightSource1.GetComponent(Light).enabled = false;	
				engineLightSource2.GetComponent(Light).enabled = false;
				engineSmoke1.GetComponent(ParticleRenderer).enabled = false;	
				engineSmoke2.GetComponent(ParticleRenderer).enabled = false;		
				engineSparks1.GetComponent(ParticleRenderer).enabled = false;	
				engineSparks2.GetComponent(ParticleRenderer).enabled = false;
				engineSounds.GetComponent(AudioSource).enabled = false;	
		if (GUI.RepeatButton(Rect(Screen.width*0.02,Screen.width*0.48,Screen.width*0.1,Screen.width*0.1),"Break")){
			Debug.Log("Clicked the button with text");
			spaceFighter.rigidbody.drag = drag;
			spaceFighter.rigidbody.drag = 0;

I suspect it has something to do with a large amount of object Find in your OnGUI().

OnGUI is pretty demanding and ‘Find’ is a slow process. As OnGUI is processed several times per frame your function looks pretty savage.

Also, the usage of for loop is perhaps not advised in this way for the same reasons.

Have a little look around and restructure, is my advice.