problem with timer

hi , i wanna destroy 3 lights when enter a trigger at the beggining one 1 sec later 2nd and 1 sec later 3rd …

i have written script but it only destroy one and nothing more happens…

public var bulb1 : Light;
public var bulb2 : Light;
public var bulb3 : Light;
var BulbDestro1 : AudioClip;
var BulbDestro2 : AudioClip;
var BulbDestro3 : AudioClip;
public var used = true;

private var startTime;


function OnTriggerEnter(other : Collider)
{
  if(other.gameObject.Tag == "Player") {
  	
  	 audio.clip = BulbDestro1; 
  	 startTime = Time.time;
  	 
	 audio.Play();
	 bulb1.enabled = false;
				
			if(startTime >= 1.0) {
				audio.clip = BulbDestro2;	
				audio.Play();
				bulb2.enabled = false;
			}
				
			if(startTime >= 2.0) {	
				audio.clip = BulbDestro3;
				audio.Play();
				bulb3.enabled = false;
				
				used = false;
			}	        
								        

      }
}

The OnTriggerEnter event gets called only once! So when you set the start time, it is set once and does not change.

You should use >>Coroutines for this kind of behavior.

public var bulb1 : Light;
public var bulb2 : Light;
public var bulb3 : Light;
var BulbDestro1 : AudioClip;
var BulbDestro2 : AudioClip;
var BulbDestro3 : AudioClip;
     
     
function OnTriggerEnter(other : Collider){
    if(other.gameObject.Tag == "Player") {
        
        destroyBulb(bulb1, BulbDestro1);

        invoke(destroyBulb(bulb2, BulbDestro2), 1);

        invoke(destroyBulb(bulb3, BulbDestro3), 2);                   

    }
}

function destroyBulb(currentBulb : Light, currentAudio : AudioClip){
    audio.clip = currentAudio;
    currentAudio.Play();

    currentBulb.enabled = false;
}

You could write a generic method to destroy a bulb and pass in the variables you want to use. Then use the Invoke method. I wrote this code example above fast on my my phone so i havn’t checked if it works but i should be something like that. You do an invoke, and pass in what method and the time before it should launch.

and oh, btw. if you read a bit about Time.time in the unity script referance you’ll see that this will return to you the time since you started the game. NOT the time when you init the var. So your code above will not start at zero and start counting up in seconds.

Time.time in Unity script reference