How to make a script affect multiple objects

I want to turn on/off multiple light sources (torch with flame effect) when the player enters certain areas within the game. I’m using the following script which works fine on a single item but I can’t figure out how to make it affect all the objects/flames at the same time?

#pragma strict

var torch : GameObject;

function Start ()
{
    torch.SetActive(false);
}

function OnTriggerEnter()
{
    torch.SetActive(true);
}

function OnTriggerExit()
{
    torch.SetActive(false);
}

(The items/objects are prefabs and untagged)

Any ideas guys?

Try this. (Pardon typos; I’m typing this directly into the post.)

#pragma strict

var torches : GameObject[];

function Start ()
{
  SetTorches(false);
}

function OnTriggerEnter()
{
  SetTorches(true);
}

function OnTriggerExit()
{
  SetTorches(false);
}

function SetTorches(value : boolean)
{
  for (var torch in torches)
  {
    torch.SetActive(value);
  }
}
1 Like