Deactivate all objects in a layer

Hi - when my player hits the first collider, I want all the objects in one layer to be activated.

But when the player hits a second collider I want all the objects in this same layer to be destroyed (or deactivated if this is easier). But I won’t need to access them after this point.

The script to activate the objects in the layer is working, but I can’t work out what I’m doing wrong in the script to destroy the objects in layer 8.

This is the script DestroyObjectsInLayer8 (JavaScript):

   var layer : int = 8;
    var layerObjects : GameObject[];
     
    
    function OnTriggerEnter (other : Collider) {
        if(other.gameObject.tag == "Player") {
           Destroy(layerObjects);
        }
    }
     
    function FindGameObjectsWithLayer (layer : int) : GameObject[] {
        var goArray = FindObjectsOfType(GameObject);
        var goList = new System.Collections.Generic.List.<GameObject>();
        for (var i = 0; i < goArray.Length; i++) {
           if (goArray*.layer == layer) {*

goList.Add(goArray*);*
}
}
if (goList.Count == 0) {
return null;
}
return goList.ToArray();
}
This is the script InstantiateObjectsInLayer8:
var layer : int = 8;
var LayerObjects : GameObject[];

function Start () {
LayerObjects = FindGameObjectsWithLayer(layer);
ActivateGameObjects(LayerObjects, false);
}

function OnTriggerEnter (other : Collider) {
if(other.gameObject.tag == “Player”) {
ActivateGameObjects(LayerObjects, true);
}
}

function FindGameObjectsWithLayer (layer : int) : GameObject[] {
var goArray = FindObjectsOfType(GameObject);
var goList = new System.Collections.Generic.List.();
for (var i = 0; i < goArray.Length; i++) {
if (goArray*.layer == layer) {*
goList.Add(goArray*);*
}
}
if (goList.Count == 0) {
return null;
}
return goList.ToArray();
}

function ActivateGameObjects (Objects : GameObject[], activate : boolean){
for(var go : GameObject in Objects){
go.active = activate;
}
}
Thanks, (sorry for the newbie question)
Laurien

Haha thats fine, gives me a great excuse to procrastinate!

Currently layerObjects doesn’t actually contain anything, you first have to add all the gameobjects into it using a similar start function:

function Start () {
    layerObjects = FindGameObjectsWithLayer(layer);
}

then when you call DestroyGameObjects(layerObjects); the list will actually contain items.