How can I link a chain of lights to Light in sequence?

Currently in the scene I have unlit torches that instantiate a lit version of itself when the player comes in range, but for bigger halls I would like to light the entire room with the first torch, and the rest light up in sequence, say like 0.5 secs after each one. I’m guessing I’d be using an Array and Invoke Repeating.

I’m having a crack at it now, just thought I would chuck this up in case i’m heading in the wrong direction and some one wants to save me.

This is the original single torch ignite via distance script I’m using at the moment.

public var litTorch : GameObject;
public var isClose : float;


public var player : GameObject;
player = GameObject.Find("Player");
function Update(){
	var distance = Vector3.Distance(player.transform.position, this.gameObject.transform.position);
	if(distance <= isClose){
	LightTorch();
	}
}



function LightTorch(){
	Instantiate(litTorch, this.gameObject.transform.position, this.gameObject.transform.rotation);
	Destroy(this.gameObject);

}

The reason I avoided WaitForSeconds is because it creates a new object each round that will have to be collected. This is no biggie here but well.

–

1 Answer

1

function LightThemAll(action:Function)
{
float timer = 0.0f;
int i = 0;
while(i < array.Length)
{
while(timer < 0.5f)
{
timer += Time.deltaTime;
yield return null
}

      action();
      i++;
      timer = 0; 
   }
}

Then you call that while getting in the room maybe with a trigger box. Make sure you call only once. It actually wont make any difference in this case but there is no reason to call hundreds of them. Your LightTorch method is passed as delegate.

var _callOnce : boolean = false;
function OnTriggerEnter(col:Collider){
    if(_callOnce == false){
        StartCoroutine(LightThemAll(LightTorch));
        _callOnce = true;
    }
}