Fire time interval problem?

Hello everyone. I have a problem. In my 2d maze game i created four bomb machine. These machines should fire bomps (Sphere) fixed time interval. (but each one is different time. Ex: first machine each two sec., second is 1sec etc.). I can do it for one sphere. But when i put sphere each machine and set fire rate and dead time on the inspector, all fire times start to run wrong and badly. This is my code:

var projectile : Transform;

var bulletSpeed : float = 20;

var destroy: float =5.0;

private var nextFireTime : float = 0.0;

private var fireRate : float = 1.0;


function Update () {


    if (Time.time - fireRate > nextFireTime)

        nextFireTime = Time.time - Time.deltaTime;


        while(nextFireTime < Time.time){

            fireProjectile();

            WaitAndDestroy();

            nextFireTime = Time.time + fireRate;

        }   
}



function fireProjectile(){

    var clone : Transform;

    clone = Instantiate(projectile, transform.position, transform.rotation);

    clone.rigidbody.AddForce(clone.transform.forward *  bulletSpeed);

}

function WaitAndDestroy()
{
  yield WaitForSeconds(destroy);

  Destroy(GameObject.Find("Roll A Ball(Clone)"));
}

Also i put spheres projectile’ roll a ball. I tried to create four different script four different roll a ball for sphere. But again i can not do it. But code run for one fire correctly. Please help me about this problem.

To get a consistent fire rate try using InvokeRepeating:

function Start()
{
	InvokeRepeating("fireLoop", 0.0, fireRate);
}
 
function fireLoop()
{
	// Code to fire a projectile
}

You should probably also destroy a specific projectile game object rather than searching the scene for the first one. You would need to pass it in as a parameter to WaitAndDestroy:

function WaitAndDestroy(projectile : GameObject)
{
	yield WaitForSeconds(destroy);
	
	Destroy(projectile);
}

I tried it thank you. but when i open roll a ball’s trigger and enter each sphere different destroy time. balls start to born and die irregular times again?

Gibbonator is close to right, this should be exactly what you should have written.

var projectile : Transform;
var bulletSpeed : float = 20;
var destroy: float =5.0;
private var fireRate : float = 1.0;
 
function Start () {
	InvokeRepeating("fireProjectile", 0, fireRate);
}
 
function fireProjectile(){
	var clone : Transform;
	clone = Instantiate(projectile, transform.position, transform.rotation);
	clone.rigidbody.AddForce(clone.transform.forward *  bulletSpeed);
	Destroy(clone, destroy);
}